Abed Aarabi
Abed Aarabi

Reputation: 149

how to return an object from a key

I am trying to return an object based on a key, I managed to do that with the method below but still, I am wondering how to return the key and value 250: [176916, 176922, 176939], instead of the value only [176916, 176922, 176939]

const myObj = {
  250: [176916, 176922, 176939],
  325: [244050],
  400: [177100],
  500: [166743],
  700: [387789],
};

let arrAcceptedValues = [];
let notArrAcceptedValues = [];
const acceptedValues = ["250", "400"];

Object.keys(myObj).forEach((key) => {
  acceptedValues.includes(key)
    ? arrAcceptedValues.push(...myObj[key])
    : notArrAcceptedValues.push(...myObj[key]);
});

console.log({ arrAcceptedValues, notArrAcceptedValues });

//arrAcceptedValues result: [{250: [176916, 176922, 176939]}, {400: [177100]}]

Upvotes: 3

Views: 315

Answers (7)

Subhash Labana
Subhash Labana

Reputation: 95

    const myObj = {
      250: [176916, 176922, 176939],
      325: [244050],
      400: [177100],
      500: [166743],
      700: [387789],
    };
    
    let arrAcceptedValues = [];
    let notArrAcceptedValues = [];
    const acceptedValues = ["250", "400"];
    
    Object.keys(myObj).forEach((key) => {
      acceptedValues.includes(key)
        ? arrAcceptedValues.push({ [key]: myObj[key]})
        : notArrAcceptedValues.push({ [key] : myObj[key]});
    });
    
    console.log({ arrAcceptedValues, notArrAcceptedValues });
// {"arrAcceptedValues":[{"250":[176916,176922,176939]},{"400":[177100]}],"notArrAcceptedValues":[{"325":[244050]},{"500":[166743]},{"700":[387789]}]}

Upvotes: 1

Utkarsh srivastava
Utkarsh srivastava

Reputation: 11

With for in loop

const myObj = {
  250: [176916, 176922, 176939],
  325: [244050],
  400: [177100],
  500: [166743],
  700: [387789],
};

let arrAcceptedValues = {};
let notArrAcceptedValues = {};
const acceptedValues = ["250", "400"];

for(let key in myObj){
  acceptedValues.includes(key)
    ? arrAcceptedValues[key] = myObj[key]
    : notArrAcceptedValues[key] = myObj[key];
}

console.log({ arrAcceptedValues, notArrAcceptedValues });

Upvotes: 1

Bhushan
Bhushan

Reputation: 106

    const myObj = {
    250: [176916, 176922, 176939],
    325: [244050],
    400: [177100],
    500: [166743],
    700: [387789],
    };

    let arrAcceptedValues = {};
    let notArrAcceptedValues = {};
    const acceptedValues = ["250", "400"];

    Object.keys(myObj).forEach((key) => {
    if (!(arrAcceptedValues[key] instanceof Array) && acceptedValues.includes(key)) {
      arrAcceptedValues[key] = [];
    }
    if (!(notArrAcceptedValues[key] instanceof Array) && !acceptedValues.includes(key)) {
      notArrAcceptedValues[key] = [];
    }
    acceptedValues.includes(key)
      ? arrAcceptedValues[key].push(...myObj[key])
      : notArrAcceptedValues[key].push(...myObj[key]);
    });

    console.log({ arrAcceptedValues, notArrAcceptedValues });`enter code here`

Upvotes: 1

Keith
Keith

Reputation: 24181

Instead of using Object.keys, you could use Object.entries this is an easy way of getting the key & the value.

eg..

const myObj = {
  250: [176916, 176922, 176939],
  325: [244050],
  400: [177100],
  500: [166743],
  700: [387789],
};

let arrAcceptedValues = {};
let notArrAcceptedValues = {};
const acceptedValues = ["250", "400"];

Object.entries(myObj).forEach(([key, value]) => {
  acceptedValues.includes(key)
    ? arrAcceptedValues[key] = value
    : notArrAcceptedValues[key] = value;
});

console.log({ arrAcceptedValues, notArrAcceptedValues });

Upvotes: 2

Saeed Shamloo
Saeed Shamloo

Reputation: 6554

Maybe this helps:

const myObj = {
  250: [176916, 176922, 176939],
  325: [244050],
  400: [177100],
  500: [166743],
  700: [387789],
};

let arrAcceptedValues = [];
let notArrAcceptedValues = [];
const acceptedValues = ["250", "400"];
Object.entries(myObj).forEach(([key,value]) =>{
    acceptedValues.includes(key)
    ? arrAcceptedValues.push({[key]: value})
    : notArrAcceptedValues.push({[key]: value});
});

console.log({arrAcceptedValues , notArrAcceptedValues })

Upvotes: 3

Harish
Harish

Reputation: 104

    Object.keys(myObj).forEach((key) => {
  acceptedValues.includes(key)
    ? arrAcceptedValues.push({ key: myObj[key]})
    : notArrAcceptedValues.push({ key: myObj[key]});
});

this will work for your case

Upvotes: 2

ProDec
ProDec

Reputation: 5410

Guess you expect something like this:

const myObj = {
  250: [176916, 176922, 176939],
  325: [244050],
  400: [177100],
  500: [166743],
  700: [387789],
};

let arrAcceptedValues = {};
let notArrAcceptedValues = {};
const acceptedValues = ["250", "400"];

Object.keys(myObj).forEach((key) => {
  acceptedValues.includes(key)
    ? arrAcceptedValues[key] = [...myObj[key]]
    : notArrAcceptedValues[key] = [...myObj[key]];
});

console.log({ arrAcceptedValues, notArrAcceptedValues });

//arrAcceptedValues result: [{250: [176916, 176922, 176939]}, {400: [177100]}]

Upvotes: 2

Related Questions