0

这是我的array下面,

Array = [
0: {id: "1",Name:'Abc'}
1: {id: "2",Name:'Abc1'} 
2: {id: "3",Name:'Abc2'}
]

现在我想{Country:'India'}在 position添加Array[2],我该如何实现。

4

2 回答 2

0

要获得最后一个位置,只需使用:array.length-1 作为数组的索引,或者您可以自己指定位置。

var arr = [{id: "1",Name:'Abc'},{id: "2",Name:'Abc1'} , {id: "3",Name:'Abc2'}];
console.log(JSON.stringify(arr));
arr[arr.length-1].country = 'morocco';
console.log(JSON.stringify(arr));

于 2019-11-22T07:57:17.387 回答
0

你可以在javascript中做这样的事情。

在此处输入图像描述

更新

arr[2] = { County: "India" };如果要覆盖位置 2 的元素,可以使用。

于 2019-11-22T07:47:24.133 回答