您不会绕过该集合来查找您正在查找的对象。 jQuery不能真正帮助。它的目的是DOM操作。如果您想要处理对象,设置,列表等功能,请查看lodash。
我写了一个函数来处理这个问题。我希望这是可以理解的。
var stuffObject = {
stuffArray1 : [{dataStuff: {stuffId: 'foobar'}}, {dataStuff: {stuffId: 'foo'}}, {}],
stuffArray2 : [{}, {dataStuff: {stuffId: 'bar'}}, {}]
}
function getObjByStuffId(stuffObject, stuffId) {
var key, arr, i, obj;
// Iterate over all the arrays in the object
for(key in stuffObject) {
if(stuffObject.hasOwnProperty(key)) {
arr = stuffObject[key];
// Iterate over all the values in the array
for(i = 0; i < arr.length; i++) {
obj = arr[i];
// And if it has the value we are looking for
if(typeof obj.dataStuff === 'object'
&& obj.dataStuff.stuffId === stuffId) {
// Stop searching and return the object.
return obj;
}
}
}
}
}
console.log('foobar?', getObjByStuffId(stuffObject, 'foobar'));
console.log('foo?', getObjByStuffId(stuffObject, 'foo'));
console.log('bar?', getObjByStuffId(stuffObject, 'bar'));