I have an array of objects containing numerous properties. I am looking to create a new array that only includes specific key-value pairs:
const sampleArray = [
{ "key1": "value1", "key2": "value2", /* … */, "key100": "value100" },
{ "key1": "value1", "key2": "value2", /* … */, "key100": "value100" },
// …
];
What's the best way to filter, map, reduce, or use another method to extract the desired keys from the array?
const newArr = sampleArray.map(function(obj) {
delete obj.key1;
delete obj.key3;
delete obj.key15;
// Long list …
return obj;
});
Is there a way to retain only key20
, key30
, key70
, key80
for each object and remove all others?