0

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

1
  • 1
    You could create a new array with the modified values using map: const newArray = a1.map((item,index)=>({...item,percent_complete:a2[index]}) Commented Dec 4, 2018 at 15:01

4 Answers 4

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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);

Comments

1
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);

2 Comments

if you mutate something then you should not use map, instead use forEach to make it clear this code is not pure and has side effects.
Fixed.. @HMR Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.