Dynamically create an array in javascript

I have an ajax request that returns some formatted JSON data. I create a Google Maps screen with it, so I need to take this data and pass it to several variables. So I want to build an array like:

var foo = [
    ['A Town', 32.844932, -50.886401, 1, setting1, '<div class="office"><div class="name">Smith</div><div class="location">111 Main Street<br /> Breen, MS<br /> 12345</div><div class="size">18 units<br />300 Foo</div><div class="thelink"><a href="#">Visit</a><br /><a href="#">Output</a></div></div>'],
    ['B Town', 33.844932, -51.886401, 2, setting1, '<div class="office"><div class="name">Jones</div><div class="location">112 Main Street<br /> Breen, MS<br /> 12345</div><div class="size">18 units<br />300 Foo</div><div class="thelink"><a href="#">Visit</a><br /><a href="#">Output</a></div></div>'],
[etc], 
[etc]
    ];

What can I use to display my Google map locations. Do I have JSON data since I go through it and create such an array? Or is there a better way to do this that I am missing (which I suspect lol)?

+3
source share
3 answers

Just do:

var foo = [];
for (/*loop*/) {
    foo.push(['this is a new array', 'with dynamic stuff']);
}
+9
source

In addition to Array.push (), you can also assign values ​​directly to Array indices. For instance,

var foo = [];

foo[0] = "Foo 0";
foo[19] = "Bob";

20 0 19.

+4

push Array .

var a = [];
var b = [1,2,3,4,5,6,7,8,9];

for (var i=0; i<b.length; i++) {
  a.push(b[i]);
}
+2

Source: https://habr.com/ru/post/1767568/


All Articles