Mustapha George
Mustapha George

Reputation: 2527

javascript array - adding entries

In javascript array called mystack, for each recordNo, I want to set up a set of lat/lon values for entities "Source1" and "Source2", but I am having trouble getting the syntax just right. recordNo is a numeric database record id (eg: 1,2,3)

    mystack = {};

    mystack[recordNo {"Source1" } ] = 
    [
        {   
            "lat": 123,
            "lon": 456
        }
    ]


    mystack[recordNo {"Source2" } ] = 
    [
        {   
            "lat": 123,
            "lon": 456
        }
    ]

Upvotes: 0

Views: 106

Answers (2)

Jon Newmuis
Jon Newmuis

Reputation: 26492

I was mixing up arrays and objects. I think I want myStack[recordNo]["Source1"] = { "lat": 123, "lon": 456 };

If this is the case, then you can do the following:

var myStack = [];
var recordNumbers = [1, 2, 3, 4];          // list of record numbers
for (var recordNo in recordNumbers) {
    myStack[recordNo] = {};
    var sources = ["Source1", "Source2"];  // list of sources

    for (var source in sources) {
        myStack[recordNo][source] = { "lat": 123, "lon": 456 };
    }
}

Upvotes: 1

xkeshav
xkeshav

Reputation: 54016

use push

eg:

   var sports = ["soccer", "baseball"];
   sports.push("football", "swimming");

Upvotes: 5

Related Questions