user8758206
user8758206

Reputation: 2191

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.

Upvotes: 0

Views: 48

Answers (3)

Alan Omar
Alan Omar

Reputation: 4217


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)

Upvotes: 2

epascarello
epascarello

Reputation: 207511

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

Upvotes: 2

Igor Nowosad
Igor Nowosad

Reputation: 639

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

Upvotes: 2

Related Questions