通过 Javascript 中的任何字段提取条目
Pull entry by any field in Javascript
我有一个包含 javascript 个对象的数组,每个对象具有三个字段,如下所示:
var people = [
{first: "john", middle: "james", last: "doe"},
{first: "jane", middle: "kate", last: "smith"},
...
{first: "kathy", middle: "rose", last: "green"},
];
我希望能够根据任何字段查询此数组,并取回匹配的对象。例如,我希望能够调用 people.getByMiddle("kate")
之类的东西并返回 {first: "jane", middle: "kate", last: "smith"}
是否有一种数据结构可以更轻松地以这种方式关联这些东西,或者我应该只编写三个单独的函数,每个函数迭代我的数据并搜索匹配项?我不想要任何依赖于数组排序的东西。
你可以定义一个辅助函数:
/**
* Filter an array of objects and return the first matching element.
* @param {array} collection - an array of objects.
* @param {string} key
* @param {string} value
* @returns {object|undefined} - the first matching object.
*/
function findWhere(collection, key, value) {
return collection.filter(function(o) {
return o[key] === value;
})[0]; // get the first matching element
};
var person = findWhere(people, 'middle', 'kate');
我只能想到这个:
var people = [
{first: "john", middle: "james", last: "doe"},
{first: "jane", middle: "kate", last: "smith"},
{first: "kathy", middle: "rose", last: "green"},
];
people.getByMiddle = function(name){
var newSelection = [];
for(i in this){
var item = this[i];
if(item.middle == name)
newSelection.push(item);
}
return newSelection;
};
people.getByMiddle("kate");
function getByProperty (arr, prop, value) {
arr.forEach(function (item) {
if (item[prop] === value) {
return item;
}
});
}
你会像这样使用它的地方:
var result = getByProperty(people, 'middle', 'kate');
这是一个可能的解决方案:
function findPeople(anArray, objProperty, searchPattern) {
return anArray.filter(function(person){
return searchPattern.test(person[objProperty]);
})
}
我有一个包含 javascript 个对象的数组,每个对象具有三个字段,如下所示:
var people = [
{first: "john", middle: "james", last: "doe"},
{first: "jane", middle: "kate", last: "smith"},
...
{first: "kathy", middle: "rose", last: "green"},
];
我希望能够根据任何字段查询此数组,并取回匹配的对象。例如,我希望能够调用 people.getByMiddle("kate")
之类的东西并返回 {first: "jane", middle: "kate", last: "smith"}
是否有一种数据结构可以更轻松地以这种方式关联这些东西,或者我应该只编写三个单独的函数,每个函数迭代我的数据并搜索匹配项?我不想要任何依赖于数组排序的东西。
你可以定义一个辅助函数:
/**
* Filter an array of objects and return the first matching element.
* @param {array} collection - an array of objects.
* @param {string} key
* @param {string} value
* @returns {object|undefined} - the first matching object.
*/
function findWhere(collection, key, value) {
return collection.filter(function(o) {
return o[key] === value;
})[0]; // get the first matching element
};
var person = findWhere(people, 'middle', 'kate');
我只能想到这个:
var people = [
{first: "john", middle: "james", last: "doe"},
{first: "jane", middle: "kate", last: "smith"},
{first: "kathy", middle: "rose", last: "green"},
];
people.getByMiddle = function(name){
var newSelection = [];
for(i in this){
var item = this[i];
if(item.middle == name)
newSelection.push(item);
}
return newSelection;
};
people.getByMiddle("kate");
function getByProperty (arr, prop, value) {
arr.forEach(function (item) {
if (item[prop] === value) {
return item;
}
});
}
你会像这样使用它的地方:
var result = getByProperty(people, 'middle', 'kate');
这是一个可能的解决方案:
function findPeople(anArray, objProperty, searchPattern) {
return anArray.filter(function(person){
return searchPattern.test(person[objProperty]);
})
}