Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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]}]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading