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
}
]
asked Dec 25, 2011 at 19:04
Mustapha George
2,54710 gold badges50 silver badges87 bronze badges
2 Answers 2
answered Dec 25, 2011 at 19:09
xkeshav
54.2k47 gold badges183 silver badges256 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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 };
}
}
answered Dec 25, 2011 at 19:10
Jon Newmuis
26.7k3 gold badges48 silver badges57 bronze badges
4 Comments
Felix Kling
Don't add non-numerical properties to arrays. Use an object instead.
Mustapha George
Right track JN! I was mixing up arrays and objects. I think I want myStack[recordNo]["Source1"] = { "lat": 123, "lon": 456 };
Felix Kling
There are still two flaws in your code: Don't use
for...in to iterate over array entries, you might be traversing other properties as well. And the nature of recordNumbers somehow suggests to use an object for myStack, not an array.Mustapha George
starting to catch on. I wll give it a try,,,
lang-js
recordNo {"Source1" }in JavaScript. However, since you didn't say what you want, we can't tell what you should do instead.mystackis not an array, it's an object.