I am receiving an array of objects as response data from an API call.
data = [{obj1},{obj2},{obj3},{obj4},{obj5}]
obj1 = {
prop1: 'value1',
prop2: 'value2',
prop3: 'value3',
}
I am trying to use an array, let's say: ['prop2', 'prop3'] to filter the array of objects and return an array of a subset of objects.
[{filteredObj1},{filteredObj2},{filteredObj3},{filteredObj4},{filteredObj5}]
filteredObj1 = {
prop2: 'value2',
prop3: 'value3'
}
And so on...
In have tried the following approach initially:
const arrayOfPropsIWant = ['prop1', 'prop2']
data.forEach((el) => {
const filtered = (({ ...arrayOfPropsIWant }) => ({ ...arrayOfPropsIWant }))(el);
})
This worked when instead of passing the ...arrayOfPropsIWant to destructure I passed the props explicitly (({ prop1, prop2 })
I am trying to build a custom ReactJS hook, is there a way I can achieve this?