2

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?

6
  • You could use array.filter to filter instead of map? Commented Oct 12, 2020 at 19:22
  • @evolutionxbox, yes whatever works. Though I thought map would be best Commented Oct 12, 2020 at 19:23
  • Map doesn't filter though. That's what filter does. --- Sorry I think I misread. You want to keep the same array size, but remove properties from each item object? Commented Oct 12, 2020 at 19:24
  • Actually you do want map here, because you want an array of the same length with transformed objects. Commented Oct 12, 2020 at 19:25
  • Basically I wand to keep the fields that are in the array Commented Oct 12, 2020 at 19:26

4 Answers 4

5

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);

Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

1
B = A.map(({ code, data_fraud, deflation, change_adaptation }) => ({ code, data_fraud, deflation, change_adaptation }))

I think this should work?

1 Comment

yes it does, but only because you've hard-coded the fields to keep. This won't work if array can be dynamic.
0

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)

1 Comment

This only returns the keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.