score:0

Accepted answer

assuming response contains the first array in your op:

getorders(orders).then(response => {
  const openedorders = response.map(order => ({ ...order, open: true}))
  this.setstate({ openedorders } )
})

response.map(order => ({ ...order, open: true}) adds a key open with the value true to every order in the array.

score:0

to add dynamic keys to a object in javascript we use [].

var x = [{ _id : 1, name: suzuki}];

x[0]['opened'] =  true;

console.log(x);

// [{ _id : 1, name: suzuki, opened: true}];

score:0

use foreach() to loop through all the orders in the array and add the desired property for each order.

let orders = [{
  _id: 1,
  name: 'honda',
}, {
  _id: 2,
  name: 'suzuki',
}, {
  _id: 3,
  name: 'audi',
}]
orders.foreach(o => o.opened = true)
console.log(orders)

score:0

you could add a new property to the objects.

var orders = [{ _id: 1, name: 'honda' }, { _id: 2, name: 'suzuki' }, { _id: 3, name: 'audi' }],
    additionalprop = { opened: true };

orders.foreach(o => object.assign(o, additionalprop));

console.log(orders);
.as-console-wrapper { max-height: 100% !important; top: 0; }

or map a new array without mutating the original objects.

var orders = [{ _id: 1, name: 'honda' }, { _id: 2, name: 'suzuki' }, { _id: 3, name: 'audi' }],
    additionalprop = { opened: true },
    result = orders.map(o => object.assign({}, o, additionalprop));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

score:2

your map should look like this. (note the return statement in map function)

orders.map(item=> {
  return {...item, opened:true}  

})

so your function could look like

getorders(orders).then(response => {
   let openedorders =  orders.map(item=> {
       return {...item, opened:true}  
     })
   this.setstate({
     openedorders
   })

}) 

Related Query

More Query from same tag