假设我有一个包含四个对象的数组:
var jsObjects = [
{a: 1, b: 2},
{a: 3, b: 4},
{a: 5, b: 6},
{a: 7, b: 8}
];
是否有一种方法,我可以通过属性b的值获得第三个对象({a: 5, b: 6})例如,没有a for…在循环?
假设我有一个包含四个对象的数组:
var jsObjects = [
{a: 1, b: 2},
{a: 3, b: 4},
{a: 5, b: 6},
{a: 7, b: 8}
];
是否有一种方法,我可以通过属性b的值获得第三个对象({a: 5, b: 6})例如,没有a for…在循环?
当前回答
使用_怎么样?找到(收集、谓语= _。[fromIndex=0])的low -dash从对象数组中通过对象属性值获取对象。你可以这样做:
var o = _.find(jsObjects, {'b': 6});
参数:
collection (Array|Object): The collection to inspect.
[predicate=_.identity] (Function): The function invoked per iteration.
[fromIndex=0] (number): The index to search from.
返回
(*): Returns the matched element (in your case, {a: 5, b: 6}), else undefined.
在性能方面,_.find()更快,因为它只提取第一个具有属性{'b': 6}的对象,另一方面,如果假设你的数组包含多个具有匹配属性集(key:value)的对象,那么你应该考虑使用_.filter()方法。因此,在您的情况下,由于您的数组只有一个具有此属性的对象,我将使用_.find()。
其他回答
你可以像下面这样使用箭头函数:
var demoArray = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
var result = demoArray.filter( obj => obj.name === 'apples')[0];
console.log(result);
// {name: 'apples', quantity: 2}
看起来在ECMAScript 6提案中有数组方法find()和findIndex()。MDN还提供了可以包含在所有浏览器中获得这些功能的腻子。
find ():
function isPrime(element, index, array) {
var start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) return false;
}
return (element > 1);
}
console.log( [4, 6, 8, 12].find(isPrime) ); // undefined, not found
console.log( [4, 5, 8, 12].find(isPrime) ); // 5
findIndex ():
function isPrime(element, index, array) {
var start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) return false;
}
return (element > 1);
}
console.log( [4, 6, 8, 12].findIndex(isPrime) ); // -1, not found
console.log( [4, 6, 7, 12].findIndex(isPrime) ); // 2
我不知道为什么你反对for循环(大概你指的是for循环,而不是特别针对..in),它们快速且易于阅读。不管怎样,这里有一些选择。
For循环:
function getByValue(arr, value) {
for (var i=0, iLen=arr.length; i<iLen; i++) {
if (arr[i].b == value) return arr[i];
}
}
.filter
function getByValue2(arr, value) {
var result = arr.filter(function(o){return o.b == value;} );
return result? result[0] : null; // or undefined
}
.forEach
function getByValue3(arr, value) {
var result = [];
arr.forEach(function(o){if (o.b == value) result.push(o);} );
return result? result[0] : null; // or undefined
}
另一方面,如果你真的想要for..in,并且想要找到一个带有任何值为6的属性的对象,那么你必须使用for..in,除非你传递名称来检查。
例子
function getByValue4(arr, value) {
var o;
for (var i=0, iLen=arr.length; i<iLen; i++) {
o = arr[i];
for (var p in o) {
if (o.hasOwnProperty(p) && o[p] == value) {
return o;
}
}
}
}
使用_怎么样?找到(收集、谓语= _。[fromIndex=0])的low -dash从对象数组中通过对象属性值获取对象。你可以这样做:
var o = _.find(jsObjects, {'b': 6});
参数:
collection (Array|Object): The collection to inspect.
[predicate=_.identity] (Function): The function invoked per iteration.
[fromIndex=0] (number): The index to search from.
返回
(*): Returns the matched element (in your case, {a: 5, b: 6}), else undefined.
在性能方面,_.find()更快,因为它只提取第一个具有属性{'b': 6}的对象,另一方面,如果假设你的数组包含多个具有匹配属性集(key:value)的对象,那么你应该考虑使用_.filter()方法。因此,在您的情况下,由于您的数组只有一个具有此属性的对象,我将使用_.find()。
如果我理解正确的话,你想在数组中找到b属性为6的对象?
var found;
jsObjects.some(function (obj) {
if (obj.b === 6) {
found = obj;
return true;
}
});
或者如果你使用下划线:
var found = _.select(jsObjects, function (obj) {
return obj.b === 6;
});