0

Consider:

var main = []

Now I want to generate many (289 to be exact) Arrays to be elements in the main one. Each of these arrays will have be like:

var subobject = {x:"A", y:"B", terrain:"C", note:"D"}

Generating the values are no problem, and I can easily put those values in a already defined subobject = {} and push(), but I can't figure out how to iterate a script each time which creates a new object and then push() into var main.

The naming of the subobject is unimportant, I'm looking for solution inwhich I can pull specific information such as:

main[0].x // get x value of the subarray in 0 location in main
main[5].note// get note value of the subarray in 5 location in main

(would it make a difference if every array had the same name? since I would never access subobject directly (after being pushed into main), but through main[X].YYY or would it have to be via main[X].subarray[Y] ?)

9
  • how so? could you explain please? Commented Aug 17, 2014 at 23:46
  • you should have used objects var subobject = {x:"A", y:"B", terrain:"C", note:"D"} Commented Aug 17, 2014 at 23:48
  • do you wish to randomize the object keys also? or the keys are the same each time? Commented Aug 17, 2014 at 23:50
  • Just use main.push(subobject)? I don't understand the problem. Commented Aug 17, 2014 at 23:52
  • @Jack If I do that, but hundreds of times, can I access and edit a particular object, despite having the same name through the array via main[0].x or so? Commented Aug 17, 2014 at 23:54

3 Answers 3

1
for (var i = 0; i < 289; i++) {
      main.push({x: getRandomX(), y: getRandomY(), terrain: getTerrain(), note: ""});
}

as long as you create new objects {} before you push them to the array it is ok. it doesn't matter if you assign the new object to the same variable (ie subobject)

you access them later like this:

main[0].x // get the value of x of the first element
Sign up to request clarification or add additional context in comments.

Comments

0

[x:"A", y:"B", terrain:"C", note:"D"] isn't valid javascript, I think you want an object here:

{x:"A", y:"B", terrain:"C", note:"D"}

And to push each generated value, you can use a for loop

for (var i = 0; i < 9; i++) {
    //do something, for example, generate a value
}

5 Comments

Thanks, I fixed that silly typo. For the loop, fair enough, but can I loop through and add a unique named array each time to be pushed? Does it even need to be a unique name?
What do you mean by "unique named array" ? I think you just want to push at the last position of the main array, that's what main.push(generatedElement) does.
Sorry, I meant uniquly named object. I'm talking about unique because I'm unsure if I would be able to generate an object with name x, push it, generate x again with different values, push it, and then be able to access both objects freely?
@user3317592 Keys in an object appended to an array will not have to be unique unless you want them to be. Test it for yourself. The array index is already unique.
if you create a new object each time before you push you will be ok. it doesn't matter that the new objects contains the same keys
0

Arrays are only numerically indexed. If you want named keys you have to use objects.

Here's the wrong way to do it.

var main = [],
    subobject = {x:"A", y:"B", terrain:"C", note:"D"};

for(var i=0; i<289; i++){

    subobject["x"] = Math.random();
    subobject["terrain"] = Math.random();
    //continue adding values using keys
    main.push(subobject);
}

The thing is if you just use the same object your going to access that object every time you iterate it, and you'll replace it's value. So you should do it like this.

var main = [],
    subobject = {};

for(var i=0; i<289; i++){
    subobject = {};//new object to make for uniquness
    subobject["x"] = Math.random();
    subobject["terrain"] = Math.random();
    //continue adding values using keys
    main.push(subobject);
}

You access members like this.

main[0].x;//value of x at index 0
//next index
main[1].terrain;//value of terrain at index 1

Collisions will only happen if you set the same index twice.

main[2].x = "value";
main[2].x = "replace value by accident";

Unless you want to change the value for some reason.

A different index will always give you a different object if you set a different one each time.

Comments

Your Answer

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.