Home > other >  How to spread inner object
How to spread inner object

Time:08-04

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 }
    ]

CodePudding user response:

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)

CodePudding user response:

[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 };
});
  • Related