Reputation: 149
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
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
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
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
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
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
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
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