0

I need to replace the order_id value with parentOrder_id in the array. When I try this, only the first record from the array is displayed.

How do I display all the entries in an array?

let nom_name = ""
let quantity = 0
let cost = 0
  
const  array = [
  {nom_name: "Test", cost: 500, quantity: 1, order_id: 1}, 
  {nom_name: "35634", cost: 100, quantity: 1, order_id: 2}
]
  
array.forEach(o => {
  nom_name =  o.nom_name,
  quantity = o.quantity,
  cost =  o.cost
})

const arr = [{
  nom_name: nom_name,
  quantity:  quantity,
  cost:  cost,
  parentOrder_id: 4
}]

console.log(arr)
  • In your example you're displaying an array with only one entry. I'm not sure of what you are trying to achieve here – clanglai Sep 28 '20 at 09:28
  • 2
    Does this answer your question? [Best way to replace key of array in JavaScript?](https://stackoverflow.com/questions/49424707/best-way-to-replace-key-of-array-in-javascript) – Always Helping Sep 28 '20 at 09:29
  • 1
    Or this one: https://stackoverflow.com/questions/6809659/changing-the-key-name-in-an-array-of-objects – Always Helping Sep 28 '20 at 09:30

3 Answers3

1

You need to map over the array like this:

const  array = [
  {nom_name: "Test", cost: 500, quantity: 1, order_id: 1}, 
  {nom_name: "35634", cost: 100, quantity: 1, order_id: 2}
]

const mapped = array.map(e => {
  const {order_id, ...rest} = e
  return ({...rest, parentOrder_id: 4})
})

console.info(mapped)

What you were doing was destructuring an array into variables so there could be only one element that way.

radulle
  • 1,437
  • 13
  • 19
  • 1
    @HarunYilmaz, ok, I now understand what you want. order_id than has to be removed through destructuring. I changed the code. – radulle Sep 28 '20 at 09:32
  • 1
    You can also use destructuring in inner function arguments like `array.map(({order_id, ...rest}) => ({...rest, parentOrder_id: 4}))` And you shouldn't hardcode `4`. It should be `parentOrder_id: order_id` – Harun Yilmaz Sep 28 '20 at 09:33
  • @HarunYilmaz I agree, this is even more elegant. – radulle Sep 28 '20 at 09:36
0

I'm not really sure what you want to do, but it seems like you want to create a new array from your old array and include the parentOrder_id which has a value of 4. To do this I would recommend using the Array.map function, which will return a new array based on a function which you pass to the map function. This would look like this:

const newArr = array.map(oldValue => {
    return {...oldValue, parentOrder_id: 4}
}

This would also leave the order_id in the newArr. If you don't want this you should not spread the old object but only take the values which you actually need.

Bob van 't Padje
  • 685
  • 1
  • 6
  • 17
0

Data structure problem

  
const  array = [
  {nom_name: "Test", cost: 500, quantity: 1, order_id: 1}, 
  {nom_name: "35634", cost: 100, quantity: 1, order_id: 2}
]
  
const arr = [];

array.forEach(o => {
  let obj = {};
  obj.nom_name =  o.nom_name;
  obj.quantity = o.quantity;
  obj.cost =  o.cost;
  obj.parentOrder_id = 4;
  arr.push(obj);
})

console.log(arr)
Mr.Thanks
  • 215
  • 3
  • 7