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.