First of all I am new to JavaScript.
I would like to insert variables into this type of array dynamically that containing latitude and longitude. Hoping for helps...
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;
Thanks in advance.
7 Answers 7
Try this -
You have to use push method to insert an object to array.
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)
2 Comments
.push() is just one of several ways to add an element to an array, and note that it doesn't "insert" so much as "append".Try this locations.push([variable_name_1,variable_name_2])
Comments
You have to declare test and test1 before locations:
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);
Comments
You can use push method to add new element in an array.
var newValues = [test,test1];
locations.push(newValues);
2 Comments
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 ]
];
or
var locations = [
[ -33.923036, 151.259052],
[ -34.028249, 151.157507],
[ -33.80010128657071, 151.28747820854187],
[-33.950198, 151.259302 ]
];
locations.push([-33.923036,151.259052])
or
var test = -33.923036; var test1 = 151.259052;
locations.push([test,test1])
console.log(locations);
1 Comment
[ `${test}`, `${test1}`] makes no sense: why would you convert the values to strings when all of the other array items are numbers?First declare your variable
var test = -33.923036; var test1 = 151.259052;
then perform push
locations.push([test,test1]);
Comments
For a dynamic insert of some values, you could wrap it in the array, you need and access as before.
If you change the value inside of loc, you get the actual value in loations as well, because you have a reference between loc and locations[0].
As long as you do not overwrite locations[0], with a new array or primitive value, you could access the actual value of 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)?locations.push([test, test1]). Or with.splice()(since you tried to use that, though.push()is a much better option):locations.splice(someIndex, 0, [test, test1]).