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

140
Views
How select items in an object from an array of undesired key and create a new object with the result

Maybe it's a weird question, and maybe that exists and other way to make that... But, I've an initial Object on which I iterate on the key. For a special task, I've needed to have this Object without some key:value. For doing that I make an object with the key that I don't want.

I make this code pen for sample: https://codepen.io/charlene-bx/pen/qBXaqEL

const myInitialObject = {
  'key#one' : 'dataofanotherObject',
  'key#two' : 'dataofanotherObject',
  'key#three' : 'dataofanotherObject',
  'key#four' : 'dataofanotherObject',
  'key#five' : 'dataofanotherObject',
  'key#six' : 'dataofanotherObject',
  'key#seven' : 'dataofanotherObject',
  'key#eight' : 'dataofanotherObject',
  'key#nine' : 'dataofanotherObject',
  'key#ten' : 'dataofanotherObject'
}

const unDesiredKey = [
  'one',
  'four',
  'six'
]

// expected output:
// {
//  'key#two' : 'dataofanotherObject',
//  'key#three' : 'dataofanotherObject',
//  'key#five' : 'dataofanotherObject',
//  'key#seven' : 'dataofanotherObject',
//  'key#eight' : 'dataofanotherObject',
//  'key#nine' : 'dataofanotherObject',
//  'key#ten' : 'dataofanotherObject'
// }

I've found this solution but I can't find it very readable:

const filteredObject = Object.keys(myInitialObject)
      .filter(key => !unDesiredKey.map( el => !key.includes(el)).some(el => el===false))
      .reduce((accumulator, value) => ({...accumulator, [value]: myInitialObject[value]}), {})
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could do this,

Use Array.prototype.map() on Object.entires() to filter out an array of desired key-value pair. And finally construct a new object from the key-value pair using Object.fromEntries()

const myInitialObject = {
    'key#one': 'dataofanotherObject',
    'key#two': 'dataofanotherObject',
    'key#three': 'dataofanotherObject',
    'key#four': 'dataofanotherObject',
    'key#five': 'dataofanotherObject',
    'key#six': 'dataofanotherObject',
    'key#seven': 'dataofanotherObject',
    'key#eight': 'dataofanotherObject',
    'key#nine': 'dataofanotherObject',
    'key#ten': 'dataofanotherObject'
}

const unDesiredKey = [
    'one',
    'four',
    'six'
]

const arr = Object.entries(myInitialObject).filter(x => !unDesiredKey.includes(x[0].split('#')[1]));

const finalObj = Object.fromEntries(arr);
console.log(finalObj)

about 4 years ago · Juan Pablo Isaza Report

0

You could filter the objects entries by keeping the ones that evaluate falsy to find(x => k.includes(x)) and use them to create a new object fromEntries.

const myInitialObject = {
  'key#one': 'dataofanotherObject',
  'key#two': 'dataofanotherObject',
  'key#three': 'dataofanotherObject',
  'key#four': 'dataofanotherObject',
  'key#five': 'dataofanotherObject',
  'key#six': 'dataofanotherObject',
  'key#seven': 'dataofanotherObject',
  'key#eight': 'dataofanotherObject',
  'key#nine': 'dataofanotherObject',
  'key#ten': 'dataofanotherObject'
}

const unDesiredKey = [
  'one',
  'four',
  'six'
];

const result = Object.fromEntries(Object.entries(myInitialObject)
  .filter(([k]) => !unDesiredKey.find(x => k.includes(x)))
);

console.log(result);

I could also accomplish this with reduce() if you like.

const obj = {
  'key#one': 'dataofanotherObject',
  'key#two': 'dataofanotherObject',
  'key#three': 'dataofanotherObject',
  'key#four': 'dataofanotherObject',
  'key#five': 'dataofanotherObject',
  'key#six': 'dataofanotherObject',
  'key#seven': 'dataofanotherObject',
  'key#eight': 'dataofanotherObject',
  'key#nine': 'dataofanotherObject',
  'key#ten': 'dataofanotherObject'
}

const unDesiredKey = [
  'one',
  'four',
  'six'
];

const result = Object.entries(obj).reduce((acc, [k, v]) => {
  if (unDesiredKey.find(x => k.includes(x))) return acc;
  acc = { ...acc, [k]: v }
  return acc;
}, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You could filter the entries and slice the key for comparing.

const
    data = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' },
    unDesiredKey = ['one', 'four', 'six'],
    result = Object.fromEntries(Object
        .entries(data)
        .filter(([key]) => !unDesiredKey.includes(key.slice(key.indexOf('#') + 1)))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!