我正在使用我的多维数组。我实际上做了几个小时的研究,但找不到任何有用的东西。
我有一个多维数组,如下所示:
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4, 0],
['Coogee Beach', -33.923036, 151.259052, 1, 1],
['Cronulla Beach', -34.028249, 151.157507, 3, 2],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2, 0],
['Maroubra Beach', -33.950198, 151.259302, 5, 2]
];
现在我想用push推送一些东西给数组:
locations.push( DATA HERE ); // 'Cronulla Beach 2', -34.028249, 151.157507, 8, 7
最后应该是这样的:
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4, 0],
['Coogee Beach', -33.923036, 151.259052, 1, 1],
['Cronulla Beach', -34.028249, 151.157507, 3, 2],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2, 0],
['Maroubra Beach', -33.950198, 151.259302, 5, 2],
['Cronulla Beach 2', -34.028249, 151.157507, 8, 7]
];
注意:我需要一个数组,所以如果它是一个带有对象的解决方案,它对我没有帮助。
如果数组仍为空,如locations[];
答案 0 :(得分:2)
由于多维数组只是一个数组数组,您可以通过将对象列表放在方括号中来创建一个新数组,您可以这样做:
locations.push(['Cronulla Beach 2', -34.028249, 151.157507, 8, 7]);
要访问或编辑现有位置,请使用其索引:
var location = locations[0];
console.log(location);
locations[0][0] = "Bomba Beach"; // change name of first location
locations[0][1] = -30.2; // change latitude of first location
答案 1 :(得分:1)
只需将内容放入另一个数组中,然后将其推送到locations
数组中,例如
locations.push(['Cronulla Beach 2', -34.028249, 151.157507, 8, 7]);