1

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.

Corné
1,4613 gold badges15 silver badges35 bronze badges
asked Apr 7, 2017 at 6:08
4
  • What is the issue ? What is console.log(locations) ? Commented Apr 7, 2017 at 6:09
  • locations.push(test); Commented Apr 7, 2017 at 6:10
  • I tried with array splice but i didnt get to know how to add array into another array? Commented Apr 7, 2017 at 6:11
  • 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]). Commented Apr 7, 2017 at 6:11

7 Answers 7

1

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)

answered Apr 7, 2017 at 6:14
Sign up to request clarification or add additional context in comments.

2 Comments

You have to use push method to insert - You don't have to use that method: .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".
Yes Indeed. there are always many options. Its just what came to my mind when I read the question :) Thanks anyways
0

Try this locations.push([variable_name_1,variable_name_2])

answered Apr 7, 2017 at 6:11

Comments

0

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);

answered Apr 7, 2017 at 6:12

Comments

0

You can use push method to add new element in an array.

var newValues = [test,test1];
locations.push(newValues);
nnnnnn
150k30 gold badges210 silver badges247 bronze badges
answered Apr 7, 2017 at 6:11

2 Comments

can we add both test and test1 together? using push
Updated the answer
0
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);
answered Apr 7, 2017 at 6:18

1 Comment

[ `${test}`, `${test1}`] makes no sense: why would you convert the values to strings when all of the other array items are numbers?
0

First declare your variable

var test = -33.923036; var test1 = 151.259052;

then perform push

locations.push([test,test1]);
answered Apr 7, 2017 at 6:23

Comments

0

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]

answered Apr 7, 2017 at 6:38

1 Comment

Thanks for the answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.