Welcome to WebmasterWorld Guest from 54.205.170.21

Forum Moderators: open

Message Too Old, No Replies

Push to Object / Array

Pushing to multidimentional "array"

   
3:10 pm on Sep 20, 2013 (gmt 0)

5+ Year Member



I've created an array object, like so:

var person = [{ age: 0, name: "" }];


I can successfully set a value if I do this:

person[0].age = 45;


But I can't seem to push a value into this object/array, like so:

person.age.push(45);


I've done this before, but I can't seem to remember how. What am I doing wrong?
3:29 pm on Sep 20, 2013 (gmt 0)



You created a multidimensional array but you push just one element. I dont know the javascript syntax but in php it would be something like this:

person[]= array(45,'');
3:33 pm on Sep 20, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



person[0] will access the first item in the array. If you want to push something onto that array, it would be:
person.push()

Your array contains an object as it's first item, so I would assume you'd want to do something like this if you were going to push to the array:

person.push({age: 0, name: "" });

For example:

var person = []; // Array is empty
person.push({age: 35, name: "John Doe"}); // Array now contains 1 item
person.push({age: 26, name: "Jane Doe"}); // Array now contains 2 items
person.push({age: 45, name: "Marty McFly"}); // Array now contains 3 items
3:45 pm on Sep 20, 2013 (gmt 0)

5+ Year Member



Thanks for your reply, Fotiman. Based on your response, I suspect I'm not creating the object properly.

I can easily accomplish my goal if I simply create two unique array variables, but I'd rather use an object so the context of the variable is more clear.

What I want is a variable called "person," with two "subvariables" attached -- one called "age" and the other called "name" -- so that I can access them in this fashion:

some_persons_age = person[5].age;


or

some_persons_name = person[22].name;


The whole variable/object/thing will be empty initially; I want to populate it via push, like so:

{ ... loop
person.age.push(sample_number);
person.name.push(sample_string);
{


Does this make sense?
4:00 pm on Sep 20, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month




What I want is a variable called "person," with two "subvariables" attached -- one called "age" and the other called "name" -- so that I can access them in this fashion:

some_persons_age = person[5].age;

or

some_persons_name = person[22].name;

What you say you want is different from what you're showing. You say you want a variable called person with 2 subvariables, which would be more like this:

some_persons_age = person.age;
some_persons_name = person.name;

But you're showing person as if it's an array of several items. For example,

some_persons_age = person[5].age; // The 6th person's age
some_persons_name = person[22].name; // The 23rd person's name

Note, using the example I gave in my previous post, that would give you this ability. For example:

var person = []; // Array is empty
person.push({age: 35, name: "John Doe"});
person.push({age: 26, name: "Jane Doe"});
person.push({age: 45, name: "Marty McFly"});

some_persons_age = person[1].age; // Jane Doe's age, 26
some_persons_name = person[2].name; // "Marty McFly"
4:07 pm on Sep 20, 2013 (gmt 0)

5+ Year Member



Ah, I must be explaining it poorly. The variable does need to be an array. For purposes of my "person" example, I will have many people, hence the need for an array.

I've just succeeded in creating what I want, but the syntax is different from what I recall doing in the past, so it nags me.

What I've done is this:

var person = { age: [], name: [] };


And I've populated it with a loop via push:

{ ... loop
person.age.push(age_value);
person.age.name(name_value);
}


Everything works as expected and makes sense; I'm only nagged because I could swear I had previously done this such that I referenced the variable like "person[x].age" instead of "person.age[x]".

I understand the difference; I was just certain that I'd done it differently before.
4:27 pm on Sep 20, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



The difference between this two is this:

With this, you've got a more "object oriented" approach, where you have an array of people objects, with each person having an age property:

person: [
{age: 0, name: ""},
{age: 0, name: ""}
]

In my opinion, that is the better model. Populating it in a loop would look like this:

{ ... loop
people.push({age: age_value, name: name_value});
}

Which I think is readable. An alternative might be:

{ ... loop
var person = {};
person.age = age_value;
person.name = name_value;
people.push(person);
}

But that's more code, and unnecessarily creates another variable that really isn't needed.

The way you're talking about doing it is not very object oriented, because you've only got a loose coupling of the age and name via their index in an array. Imagine if you were to try and populate it this way:


var person = {
age: [
35,
26,
45
],
name: [
"John Doe",
"Jane Doe",
"Marty McFly"
]
}

It becomes much less obvious which age corresponds to which name, especially as the list grows.

Personally, I would not go with this second format for the sole purpose of how the "push" execution looks.
4:33 pm on Sep 20, 2013 (gmt 0)

5+ Year Member



Aha! You gave me exactly what I was after.


{ ... loop
people.push({age: age_value, name: name_value});
}


This is the syntax that I couldn't recall. I much prefer this way as well, since it is indeed more object oriented. Thank you :)
4:37 pm on Sep 20, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



That's basically the same syntax I posted in my first reply. :) Either way, glad your problem is solved. :)
8:37 pm on Sep 20, 2013 (gmt 0)

5+ Year Member



Fotiman, please pardon me if its frowned upon to reply to an existing answer with a new question, but this is related and you seem to be an authority on the matter.

Considering the object structure I've got (using the same "person" example), how do I find a value in this array?

For example, in a simple array, to find the key of the first person with a value of 25, I'd do this:

age_val = person.indexOf(25);


But I can't seem to find the syntax to do this with a "complex" array like this object.

I've tried:

age_val = person.age.indexOf(25);


and

age_val = person.indexOf({age: 25});


but neither works.

I could of course use a loop to step through the array and examine each value, but that feels sloppy and inefficient.

Is it even possible to use indexOf in this fashion? If not, is there another way to search this type of object/array for a specific value?
11:51 pm on Sep 20, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Right, you won't be able to use indexOf the way you're trying to. Also, I'm assuming that "age" is not a unique value, but you could potentially create an additional "hash map" for looking up people by their age. For example:

var age,
personMapByAge = {
find: function(age) {
return this[age] || [];
}
},
i,
n;

for (i = 0, n = person.length; i < n; i++) {
age = person[i].age;
personMapByAge[age] = personMapByAge[age] || [];
personMapByAge[age].push(person[i]);
}

Then to find people by age, you could just do:

var people_age_26 = personMapByAge.find(26); // returns an array

You can check the length of the return value to see how many there are (since it returns an array).

Also, in this example I built the map by iterating over the person array, but you could build up the map in the same loop that you build the array to be more efficient:


{ ... loop
people.push({age: age_value, name: name_value});
personMapByAge[age] = personMapByAge[age] || [];
personMapByAge[age].push(person[i]);
}


Hope that helps.
4:03 am on Sep 21, 2013 (gmt 0)

5+ Year Member



It is indeed helpful, thank you. Not the simplistic answer I was hoping for, but helpful nonetheless :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month