gpt4 book ai didi

jquery - 在现有数组中添加额外的元素和值

转载 作者:行者123 更新时间:2023-12-01 04:35:57 26 4
gpt4 key购买 nike

我有一个像这样的数组

details:{ 0: {TruckAllocationId: 1, RefTaskId: 3, RefTruckId: 7, TruckNo: "TH56445565", DriverName: "driver"} 1: {TruckAllocationId: 2, RefTaskId: 3, RefTruckId: 9, TruckNo: "88989798990", DriverName: "dasdah"} }

我必须在上面的数组中添加更多值

示例我想添加新元素,并且值第 0 个和第 1 个数组年龄添加到下面的数组中

0: {TruckAllocationId: 1, RefTaskId: 3, RefTruckId: 7, TruckNo: "TH56445565", DriverName: "driver",Age:16} 1: {TruckAllocationId: 2, RefTaskId: 3, RefTruckId: 9, TruckNo: "88989798990", DriverName: "dasdah",Age:18}

我尝试下面的代码它不起作用

if (typeof rowA.details != 'undefined')
{
for (var i = 0; i < rowA.details.length; i++) {
rowA.details[i].push({
WheeloadermapFlag: _JsonValue["Table"[counter].WheeloadermapFlag
, TruckAllocationStatus: _JsonValue["Table"][counter].TruckAllocationStatus
, NoofTruckRequired: _JsonValue["Table"][counter].NoofTruckRequired
, MappedTruck: _JsonValue["Table"][counter].MappedTruck
,
})
}
}

帮我解决上述问题

最佳答案

名为“details”的对象不是数组 - 它是对象。如果您想保留它,只需使用属性名称作为字符串即可。就像“0”:{....}。因此,您应该使用对象属性来更改/删除/添加值。例如 rowA.details[i].DriverName = "Name"rowA.details[i]["DriverName"] = "Name"

如果您不想保留对象的“详细信息”,请删除属性并将其设为实际数组。然后你的代码就可以工作了。例如details = [{}, {}, ...]

关于jquery - 在现有数组中添加额外的元素和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55393058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com