joe
joe

Reputation: 59

how to insert data into an array of objects javascript

Can someone please help me with some code to insert data into an existing array of objects so i have this array

[
  {
    "order_id": "241918",
    "customer_name": "Marietjie",
    "customer_surname": "Short",
    "total_items": "44",
    "completed_items": "17",
    "percent_complete": 0.38636363636364,
    "datetime_received": "2018-07-25 15:18:25",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": "joel"
  },
  {
    "order_id": "281774",
    "customer_name": "Ashleigh",
    "customer_surname": "Hodge",
    "total_items": "16",
    "completed_items": "0",
    "percent_complete": 0,
    "datetime_received": "2018-10-04 15:59:19",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": null
  }
]

i want to insert data from this array into the array above to replace the value of the percent_completed with the value of the array below.

["17", "0"]

so for the first

Upvotes: 0

Views: 2397

Answers (4)

jShubh
jShubh

Reputation: 79

const array =[
  {
    "order_id": "241918",
    "customer_name": "Marietjie",
    "customer_surname": "Short",
    "total_items": "44",
    "completed_items": "17",
    "percent_complete": 0.38636363636364,
    "datetime_received": "2018-07-25 15:18:25",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": "joel"
  },
  {
    "order_id": "281774",
    "customer_name": "Ashleigh",
    "customer_surname": "Hodge",
    "total_items": "16",
    "completed_items": "0",
    "percent_complete": 0,
    "datetime_received": "2018-10-04 15:59:19",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": null
  }
];
const array1= ["17","12"];

result = array1.map((value, index) => {

return { ...array[index], completed_items: value };
});
console.log(result);

Upvotes: 1

Doina Bostan
Doina Bostan

Reputation: 74

Try this:

	var array=[
  {
    "order_id": "241918",
    "customer_name": "Marietjie",
    "customer_surname": "Short",
    "total_items": "44",
    "completed_items": "17",
    "percent_complete": 0.38636363636364,
    "datetime_received": "2018-07-25 15:18:25",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": "joel"
  },
  {
    "order_id": "281774",
    "customer_name": "Ashleigh",
    "customer_surname": "Hodge",
    "total_items": "16",
    "completed_items": "0",
    "percent_complete": 0,
    "datetime_received": "2018-10-04 15:59:19",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": null
  }
]

var array2=["17", "0"];

for(var x=0; x< array.length; x++){
	array[x].percent_complete=array2[x]
}

console.log(array)

Upvotes: 1

Cid
Cid

Reputation: 15247

Loop over the array containing the new values and the one containing the datas (Loop will stops when the end of one of them will be reached), then update the data one at each iteration.

let myarr = [
  {
    "order_id": "241918",
    "customer_name": "Marietjie",
    "customer_surname": "Short",
    "total_items": "44",
    "completed_items": "17",
    "percent_complete": 0.38636363636364,
    "datetime_received": "2018-07-25 15:18:25",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": "joel"
  },
  {
    "order_id": "281774",
    "customer_name": "Ashleigh",
    "customer_surname": "Hodge",
    "total_items": "16",
    "completed_items": "0",
    "percent_complete": 0,
    "datetime_received": "2018-10-04 15:59:19",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": null
  }
];

let newvals = ["17", "0"];

for (let i = 0; i < newvals.length && i < myarr.length; ++i)
  myarr[i].percent_complete = newvals[i];

console.log(myarr);

Upvotes: 1

Goran.it
Goran.it

Reputation: 6299

You just need to iterate your original data array and overwrite it's property with according value from the provided update array. Something like this:

var data = [
  {
    "order_id": "241918",
    "customer_name": "Marietjie",
    "customer_surname": "Short",
    "total_items": "44",
    "completed_items": "17",
    "percent_complete": 0.38636363636364,
    "datetime_received": "2018-07-25 15:18:25",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:0-0",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": "joel"
  },
  {
    "order_id": "281774",
    "customer_name": "Ashleigh",
    "customer_surname": "Hodge",
    "total_items": "16",
    "completed_items": "0",
    "percent_complete": 0,
    "datetime_received": "2018-10-04 15:59:19",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": null
  }
];
var update = ["17", "0"];
data.forEach((d, i) => {
  data[i].percent_completed = update[i]
})

Upvotes: 1

Related Questions