简体   繁体   中英

How to spread inner object

I have data like this

finalValue [
  { _id: { _id: 'OUTCOME_APPROUVED' }, count: 1 },
  { _id: { _id: 'OUTCOME_SWITCH_INTERFACE' }, count: 5 }
]

I want to spread the inner object and change the keys to name and value to make final value looks like this

   finalValue [
      {   name: 'OUTCOME_APPROUVED' , value: 1 },
      {  name: 'OUTCOME_SWITCH_INTERFACE' , value: 5 }
    ]

try this:

 var finalValue = [ { _id: { _id: 'OUTCOME_APPROUVED' }, count: 1 }, { _id: { _id: 'OUTCOME_SWITCH_INTERFACE' }, count: 5 } ] var newValue = finalValue.map(({_id:{_id},count}) => { return {name:_id, value:count} }) console.log(newValue)

[JS] You could use map instead of spreading, and not sure about how you could spread it to format it the way you want.

const inputValue = [ { _id: { _id: 'OUTCOME_APPROUVED' }, count: 1 },
  { _id: { _id: 'OUTCOME_SWITCH_INTERFACE' }, count: 5 }]

const computedValue = inputValue.map((data) => {
  return { name: data._id._id, value: data.count };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM