简体   繁体   中英

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.

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


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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM