Это прямо вперед, чтобы сделать это.Если вам нужен быстрый доступ несколько раз, вы должны создать карту, основанную на имени свойства, по которому вы ищете.
Вот функция, которая принимает массивы и строит карты с ключами.Он не универсален, но вы можете изменить его для собственного использования.
/**
* Given an array and a property name to key by, returns a map that is keyed by each array element's chosen property
* This method supports nested lists
* Sample input: list = [{a: 1, b:2}, {a:5, b:7}, [{a:8, b:6}, {a:7, b:7}]]; prop = 'a'
* Sample output: {'1': {a: 1, b:2}, '5': {a:5, b:7}, '8': {a:8, b:6}, '7':{a:7, b:7}}
* @param {object[]} list of objects to be transformed into a keyed object
* @param {string} keyByProp The name of the property to key by
* @return {object} Map keyed by the given property's values
*/
function mapFromArray (list , keyByProp) {
var map = {};
for (var i=0, item; item = list[i]; i++) {
if (item instanceof Array) {
// Ext.apply just copies all properties from one object to another,
// you'll have to use something else. this is only required to support nested arrays.
Ext.apply(map, mapFromArray(item, keyByProp));
} else {
map[item[keyByProp]] = item;
}
}
return map;
};