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]}]
>Solution :
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 })