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

246
Views
Remove entries from an object where the keys aren't in an array

I'm trying to remove entries from an object so that it only includes those that are in an array. So I have a required array:

const required = ['accountNumber', 'packRequested', 'collectionDate']

And a touchedFields object:

const touchedFields = {
    accountNumber: true,
    sortCode: false,
    account: true,
    collectionDate: false,
    packRequested: false
}

This is what I am after:

const touchedFields = {
        accountNumber: true,
        collectionDate: false,
        packRequested: false
    }

How can this be achieved?

I've tried instead creating an array output (as I couldn't do the object), looping through each object entry, and the looping through each array key, comparing values and pushing to a new array if the values matches:

let newArr = []
for (let [key, value] of Object.entries(requiredFields)) {
    Object.keys(touchedFields).forEach((cur) => {
        if (cur == value) {
            newArr.push({cur: value})
        }
    })
}
console.log(newArr)

But even that doesn't set the values properly as it set's the key as cur and not the actual cur variable:

[{cur: 'accountNumber'}, {cur: 'packRequested'}, {cur: 'collectionDate'}]

Any help would be greatly appreciated.

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

0


const required = ['accountNumber', 'packRequested', 'collectionDate']

const touchedFields = {
    accountNumber: true,
    sortCode: false,
    account: true,
    collectionDate: false,
    packRequested: false
}

let result = Object.fromEntries(
  Object.entries(touchedFields)
  .filter(([k,v]) => required.includes(k))
)

console.log(result)
about 4 years ago · Juan Pablo Isaza Report

0

Use Array.reduce method

const required = ['accountNumber', 'packRequested', 'collectionDate']

const touchedFields = {
  accountNumber: true,
  sortCode: false,
  account: true,
  collectionDate: false,
  packRequested: false
}

const result = required.reduce((acc, key) => {
  acc[key] = touchedFields[key];
  return acc;
}, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

If you want to alter the orginal object, you can loop over the keys and delete the ones that are not in the required array.

const required = ['accountNumber', 'packRequested', 'collectionDate']

const touchedFields = {
  accountNumber: true,
  sortCode: false,
  account: true,
  collectionDate: false,
  packRequested: false
}

const pruneObject = (obj, keys) => Object.keys(obj).forEach(
  key => !required.includes(key) && delete obj[key]
);

pruneObject(touchedFields, required);
console.log(touchedFields);

If you do not want to alter the original object you can loop over the array and build a new object from it.

const required = ['accountNumber', 'packRequested', 'collectionDate']

const touchedFields = {
  accountNumber: true,
  sortCode: false,
  account: true,
  collectionDate: false,
  packRequested: false
}

const inclusivePick = (obj, keys) => Object.fromEntries(
  keys.map(key => [key, obj[key]])
);


const result = inclusivePick(touchedFields, required);
console.log(result);

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!