I am trying to filter these 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
}]
By this array:
array=["data_fraud","change_adaptation", "deflation","code"]
The result I am looking for is:
B= [{ code: "CH",
data_fraud: 13,
deflation: 4,
change_adaptation: 9
},
{
code: "TZ"
data_fraud: 10,
deflation: 3,
change_adaptation: 7
}]
I have done this:
B = A.map(({ ...array }) => ({ ...array }))
But this is not working. I know map should do the work but how can I list the fields of the objects I want to filter out?
array.filterto filter instead ofmap?maphere, because you want an array of the same length with transformed objects.