0

My question:

I have an array of objects as such

var people = [
        {name: 'Dan'  ,  age: '20', favoriteColor : 'blue'}, 
        {name: 'Bob'  ,  age: '35', favoriteColor : 'red' }, 
        {name: 'Frank',  age: '22', favoriteColor : 'green'}, 
        {name: 'Ed'   ,  age: '27', favoriteColor : 'yellow'}
]

I need to be able to identify the objects in the array by their name property. [It's important to note that the name property for each object in the array WILL be unique]. However, they are not ordered by any particular pattern. So people[0] may or may not equal the object with a 'name' of 'Dan'.

I want to be able to access the data by organizing it so that

people.Dan 

returns the object

{age: '20', favoriteColor: 'blue'}

I feel like this should be relatively simple, but I don't really know the words to describe the issue to be able to find the proper solution.

EDIT: For anyone reading this in the future, I decided to go with Elliot's solution instead of using underscore.js. They both solve the problem, but it ended up being easier just to implement another function.

3 Answers 3

3

Right now people is an array. You need to restructure it into an object, where each name is a property returning the rest of that object. Here's one way to do that:

var tempPeople = {};
for(var i = 0, len = people.length; i<len; i++) {
    tempPeople[people[i].name] = people[i];
}

people = tempPeople;

You could also leave people as an array and instead use Array.filter to select by name:

var dan = people.filter(function(person) { return person.name == "Dan" })[0]; // returns the first person named Dan
Sign up to request clarification or add additional context in comments.

Comments

1

You should use underscore.js or lodash. They have a method called findWhere which does exactly what you need. The method _.where will return an array if there is more than one matching object.

_.findWhere(people, {name: "dan"});
// will return
// {name: 'Dan', age: '20', favoriteColor: 'blue'}

7 Comments

It's generally bad form to recommend including an entire library for the use of one function. Furthermore the OP is working with Node.js, and it can be a pain to get Underscore to work with Node.
@ElliotBonneville I disagree, first, lodash is super easy to work with in node, and second, these are small utility libraries that are extremely useful. If OP doesn't have a utility-belt library which can do what he is asking, he should.
I've have never used underscore.js, but I've heard a lot of positive feedback from people that have used it/written about it on the web. Not sure if I should go this route, just for the sake of checking it out, even though the solution provided by Elliot Bonneville looks solid as well...
Well, @Dan, I would definitely not discourage you from checking out Underscore and/or lodash. They're both useful libraries and you'd get a lot of mileage out of either of them. However, you might directly learn more if you went and copied out findWhere from one of them to see how it works (you'll likely find it's similar to the solution I provided).
Elliot's solution is great, but you need to abstract it so that the code can be used in many situations, whenever you need to do this kind of thing, you can either do this yourself, or just use a library.
|
-1

Make an object constructor. Write a custom method.

function person(name,age,favoriteColor) {
    this.name = name;
    this.age = age;
    this.favoriteColor = favoriteColor;
    this.getInfo = function() {
        return this.age + " - " + this.favoriteColor;
    }
}

var people = [];

people.push(new person('dan','20','blue'));

alert(people[0].getInfo());

3 Comments

This doesn't allow the OP to select users by name, however, which is what he was looking to do. -1
Yes it freakin does. just alert(people[0].name);
Whoops, sorry, just got it.

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.