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'.
我需要能够通过name属性识别数组中的对象。 [值得注意的是,数组中每个对象的name属性将是唯一的]。但是,它们不按任何特定模式排序。所以人们[0]可能会或可能不会将该对象与“丹”的“名称”相等。
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.
编辑:对于将来读这篇文章的人,我决定采用Elliot的解决方案,而不是使用underscore.js。他们都解决了这个问题,但最终只是为了实现另一个功能而变得容易。
3 个解决方案
#1
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.
你应该使用underscore.js或lodash。他们有一个名为findWhere的方法,它可以完全满足您的需求。如果有多个匹配对象,则_.where方法将返回一个数组。
_.findWhere(people, {name: "dan"});
// will return
// {name: 'Dan', age: '20', favoriteColor: 'blue'}
#2
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:
您也可以将人员保留为数组,而是使用Array.filter按名称进行选择:
var dan = people.filter(function(person) { return person.name == "Dan" })[0]; // returns the first person named Dan
#3
-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());
#1
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.
你应该使用underscore.js或lodash。他们有一个名为findWhere的方法,它可以完全满足您的需求。如果有多个匹配对象,则_.where方法将返回一个数组。
_.findWhere(people, {name: "dan"});
// will return
// {name: 'Dan', age: '20', favoriteColor: 'blue'}
#2
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:
您也可以将人员保留为数组,而是使用Array.filter按名称进行选择:
var dan = people.filter(function(person) { return person.name == "Dan" })[0]; // returns the first person named Dan
#3
-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());