假设我有一个包含四个对象的数组:

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…在循环?


当前回答

实现需求的方法:

使用Array.find()方法:

const jsObject = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ]; const filteredResult = jsObject.find((e) => e.b == 6); console.log (filteredResult);

使用Array.filter()方法:

const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ]; const filterObj = jsObjects.filter((e) => e.b == 6); console.log (filterObj [0]);

使用……循环中:

const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ]; for (const i in jsObjects) { 如果(jsObjects[我]。B == 6) { console.log (jsObjects[我]); } }

其他回答

好吧,有几种方法可以做到这一点,但让我们从最简单也是最新的方法开始,这个函数叫做find()。

当你使用find to时要小心,因为即使是IE11也不支持它,所以它需要被转译…

你有了这个物体

var jsObjects = [
   {a: 1, b: 2}, 
   {a: 3, b: 4}, 
   {a: 5, b: 6}, 
   {a: 7, b: 8}
];

你可以这样写一个函数:

function filterValue(obj, key, value) {
  return obj.find(function(v){ return v[key] === value});
}

然后像这样使用函数:

filterValue(jsObjects, "b", 6); //{a: 5, b: 6}

在ES6中也有缩短版本:

const filterValue = (obj, key, value)=> obj.find(v => v[key] === value);

该方法只返回第一个匹配…,为了获得更好的结果和浏览器支持,你可以使用filter:

const filterValue = (obj, key, value)=> obj.filter(v => v[key] === value);

返回[{a: 5, b: 6}]…

此方法将返回一个数组…

你也可以简单地使用for循环,创建一个这样的函数:

function filteredArray(arr, key, value) {
  const newArray = [];
  for(i=0, l=arr.length; i<l; i++) {
    if(arr[i][key] === value) {
      newArray.push(arr[i]);
    }
  }
 return newArray;
}

像这样叫它:

filteredArray(jsObjects, "b", 6); //[{a: 5, b: 6}]

如果您正在寻找一个单一的结果,而不是一个数组,我是否可以建议减少?

这里是一个简单的'ole javascript解决方案,如果存在,返回一个匹配的对象,如果不存在,则返回null。

var result = arr.reduce(function(prev, curr) { return (curr.b === 6) ? curr : prev; }, null);

使用_怎么样?找到(收集、谓语= _。[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()。

看起来在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
var result = jsObjects.filter(x=> x.b === 6);

会更好,在过滤器中使用return有时你无法得到结果(我不知道为什么)