0

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.

user8758206
  • 2,106
  • 4
  • 22
  • 45
  • Does this answer your question? [How do I create a dynamic key to be added to a JavaScript object variable](https://stackoverflow.com/questions/2462800/how-do-i-create-a-dynamic-key-to-be-added-to-a-javascript-object-variable) – computercarguy Dec 09 '21 at 15:47

3 Answers3

2

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);
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Igor Nowosad
  • 529
  • 3
  • 9
2

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);
epascarello
  • 204,599
  • 20
  • 195
  • 236
2

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)
Alan Omar
  • 4,023
  • 1
  • 9
  • 20