I have a challenge filtering specific fields from a set of JavaScript objects:
A= [{
asset_bubble: 17,
biodiversity_loss: 15,
code: "CH",
critical_information: 14,
cyber_attacks: 19,
data_fraud: 13,
deflation: 4,
energy: 18,
extreme_weather: 12,
change_adaptation: 9,
infrastructure: 33
},
{
asset_bubble: 4,
biodiversity_loss: 7,
code: "TZ"
critical_information: 9,
cyber_attacks: 9,
data_fraud: 10,
deflation: 3,
energy: 1,
extreme_weather: 2,
change_adaptation: 7
infrastructure: 3
}]
The goal is to filter out these fields using the following array:
array=["data_fraud","change_adaptation", "deflation","code"]
The desired result should be:
B= [{ code: "CH",
data_fraud: 13,
deflation: 4,
change_adaptation: 9
},
{
code: "TZ"
data_fraud: 10,
deflation: 3,
change_adaptation: 7
}]
My attempt involved using the map function as follows:
B = A.map(({ ...array }) => ({ ...array }))
However, this approach did not yield the expected output. I am aware that the map method should work but how can I correctly specify the fields to be filtered from the objects?