嗨所以我得到了这个数组:
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
并且我想要附加它就像它是示例:
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1],
['New item', -22.950198, 141.259302, 6]
];
我不知道该怎么做,搜索“将项目添加到数组中的数组”似乎没有用。
答案 0 :(得分:1)
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var newLocation = ['New item', -22.950198, 141.259302, 6];
locations.push(newLocation);
答案 1 :(得分:1)
locations.push(['New item', - 22.950198,141.259302,6]); 是正确的答案。
答案 2 :(得分:0)
如果你想在最后添加,你可以使用push()方法! 或者你可以使用.unshift方法添加第一个!在这里查看更多信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
答案 3 :(得分:0)
尝试使用它:
var locations = [
{'Bondi Beach', -33.890542, 151.274856, 4},
{'Coogee Beach', -33.923036, 151.259052, 5},
{'Cronulla Beach', -34.028249, 151.157507, 3},
{'Manly Beach', -33.80010128657071, 151.28747820854187, 2},
{'Maroubra Beach', -33.950198, 151.259302, 1}
];
现在您可以应用推送
locations.push({'New item', -22.950198, 141.259302, 6});
让我知道它是否有效。
答案 4 :(得分:0)
或者您可以使用spread syntax
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
locations = [...locations,['New item', -22.950198, 141.259302, 6]];
console.log(locations);