I want a nice small one liner to use an array, for example ['postcode','town']
, and then pluck just these properties from a larger object.
So this:
const fields = ['postcode','town'];
const obj = {
name: 'test',
postcode: 'SS2 5JJ',
town: 'Somewhere',
other: 'info'
}
Would become this:
const filtered = {
postcode: 'SS2 5JJ',
town: 'Somewhere',
}
I did find this however I have mis-understood it I believe:
const fields = ['postcode','town'];
Object.keys(fields).map(e => fields[e])
What is the best way to do this?