0

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
3
  • 2
    Your syntax makes no sense. What are you trying to do? Commented Dec 25, 2011 at 19:07
  • You can never say recordNo {"Source1" } in JavaScript. However, since you didn't say what you want, we can't tell what you should do instead. Commented Dec 25, 2011 at 19:09
  • mystack is not an array, it's an object. Commented Dec 25, 2011 at 19:11

2 Answers 2

5

use push

eg:

 var sports = ["soccer", "baseball"];
 sports.push("football", "swimming");
answered Dec 25, 2011 at 19:09
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

Don't add non-numerical properties to arrays. Use an object instead.
Right track JN! I was mixing up arrays and objects. I think I want myStack[recordNo]["Source1"] = { "lat": 123, "lon": 456 };
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.
starting to catch on. I wll give it a try,,,

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.