Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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

this will work for your case

about 4 years ago · Juan Pablo Isaza Report

0

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 });

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!