有人可以帮我一些代码,将数据插入到现有的对象数组中,这样我就拥有了这个数组
[
{
"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
}
]
我想将此数组中的数据插入上方的数组,以将percent_completed
的值替换为下方的数组的值。
["17", "0"]
第一个
答案 0 :(得分: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);
答案 1 :(得分:0)
您只需要迭代原始数据数组,并使用提供的更新数组中的相应值覆盖其属性即可。像这样:
PreferenceFragment
答案 2 :(得分:0)
在包含新值和包含数据的数组上循环(当到达其中一个的末尾时,循环将停止),然后在每次迭代时更新一个数据。
CREATE TYPE name_type AS ENUM('name1', 'name2');
CREATE TABLE Employee2(
ID integer,
myname name_type
);
答案 3 :(得分:0)
尝试一下:
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)