使用Javascript添加数组元素
首先我是java脚本新手。使用Javascript添加数组元素
我想将变量插入到包含经度和纬度的动态数组中。希望有帮助...
var locations = [
[ test, test1],
[ -33.923036, 151.259052],
[ -34.028249, 151.157507],
[ -33.80010128657071, 151.28747820854187],
[-33.950198, 151.259302 ]
];
var test = -33.923036; var test1 = 151.259052;
在此先感谢。
试试这个 -
您必须使用push方法将对象插入到数组中。
var test = -33.923036; var test1 = 151.259052;
var locations = [
[ -33.923036, 151.259052],
[ -34.028249, 151.157507],
[ -33.80010128657071, 151.28747820854187],
[-33.950198, 151.259302 ]
];
locations.push([test, test1])
console.log(locations)
您可以使用push方法在数组中添加新元素。
var newValues = [test,test1];
locations.push(newValues);
我们可以同时添加test和test1吗?使用推 –
更新了答案 –
感谢它的工作 –
试试这个locations.push([variable_name_1,variable_name_2])
感谢它的工作 –
你必须locations
之前声明test
和test1
:
var test = 1, test1 = 2;
var locations = [
[ test, test1],
[ -33.923036, 151.259052],
[ -34.028249, 151.157507],
[ -33.80010128657071, 151.28747820854187],
[-33.950198, 151.259302 ]
];
console.log(locations);
var test = -33.923036; var test1 = 151.259052;
var locations = [
[ test, test1],
[ -33.923036, 151.259052],
[ -34.028249, 151.157507],
[ -33.80010128657071, 151.28747820854187],
[-33.950198, 151.259302 ]
];
或
var locations = [
[ -33.923036, 151.259052],
[ -34.028249, 151.157507],
[ -33.80010128657071, 151.28747820854187],
[-33.950198, 151.259302 ]
];
locations.push([-33.923036,151.259052])
或
var test = -33.923036; var test1 = 151.259052;
locations.push([test,test1])
console.log(locations);
'[\'$ {test} \','''{test1} \']'没有任何意义:为什么要将值转换为字符串当所有其他数组项是数字? – nnnnnn
首先声明你的变量
var test = -33.923036; var test1 = 151.259052;
然后进行推
locations.push([test,test1]);
对于一些价值的动态插入,你可以在阵列中把它包起来,你需要像以前访问。
如果更改loc
的内部值,则同样会得到loations
中的实际值,因为您在loc
和locations[0]
之间有一个参考。
只要不用locations[0]
覆盖locations[0]
,使用新的数组或原始值,就可以访问实际值loc
。
var loc = [
-33.923036,
151.259052
],
locations = [
loc,
[-33.923036, 151.259052],
[-34.028249, 151.157507],
[-33.80010128657071, 151.28747820854187],
[-33.950198, 151.259302 ]
];
console.log(locations[0][0]); // -33.923036
loc[0] = 42;
console.log(locations[0][0]); // 42
locations[0][0] = -10;
console.log(locations[0][0]); // -10
console.log(loc); // [-10, 151.259052]
感谢您的回答。 –
问题是什么?什么是'console.log(locations)'? – Rayon
locations.push(test); –
我试着用数组拼接,但我没有去知道如何将数组添加到另一个数组? –