Reputation: 479
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?
Upvotes: 2
Views: 211
Reputation: 5380
based on Derek's answer I made handy function to filter object by its keys:
function filterBykeys ( target, keyList ) {
return Object.fromEntries(
Object.entries(target).filter(
([key,value]) => keyList.includes(key)
)
)
}
and here is a snippet using this function
function filterBykeys ( target, keyList ) {
return Object.fromEntries(
Object.entries(target).filter(
([key,value]) => keyList.includes(key)
)
)
}
const data= [{
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
}];
const arrays=["data_fraud","change_adaptation", "deflation","code"];
const result = data.map( obj => {
return filterBykeys(obj, arrays);
});
console.log(result)
Upvotes: 0
Reputation: 18249
Here's another approach, using reduce
:
const 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
}];
const array=["data_fraud","change_adaptation", "deflation","code"];
const B = A.map(item => array.reduce((acc, key) => ({ ...acc, [key]: item[key]}), {}));
console.log(B);
Upvotes: 2
Reputation: 1563
B = A.map(({ code, data_fraud, deflation, change_adaptation }) => ({ code, data_fraud, deflation, change_adaptation }))
I think this should work?
Upvotes: 1
Reputation: 10194
Inside Array.map
callback, you can map array
variable values to [key, item[key]]
pair and from that 2d array, you can generate the object using Object.fromEntries
as follows.
const 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
}];
const array = ["data_fraud","change_adaptation", "deflation","code"];
const B = A.map((item) => (
Object.fromEntries(array.map((key) => ([key, item[key]])))
));
console.log(B);
Upvotes: 5