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

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


当前回答

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

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

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

其他回答

通过特定的属性值从对象数组中获取第一个对象:

function getObjectFromObjectsArrayByPropertyValue(objectsArray, propertyName, propertyValue) { return objectsArray.find(function (objectsArrayElement) { return objectsArrayElement[propertyName] == propertyValue; }); } function findObject () { var arrayOfObjectsString = document.getElementById("arrayOfObjects").value, arrayOfObjects, propertyName = document.getElementById("propertyName").value, propertyValue = document.getElementById("propertyValue").value, preview = document.getElementById("preview"), searchingObject; arrayOfObjects = JSON.parse(arrayOfObjectsString); console.debug(arrayOfObjects); if(arrayOfObjects && propertyName && propertyValue) { searchingObject = getObjectFromObjectsArrayByPropertyValue(arrayOfObjects, propertyName, propertyValue); if(searchingObject) { preview.innerHTML = JSON.stringify(searchingObject, false, 2); } else { preview.innerHTML = "there is no object with property " + propertyName + " = " + propertyValue + " in your array of objects"; } } } pre { padding: 5px; border-radius: 4px; background: #f3f2f2; } textarea, button { width: 100% } <fieldset> <legend>Input Data:</legend> <label>Put here your array of objects</label> <textarea rows="7" id="arrayOfObjects"> [ {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}, {"a": 7, "b": 8, "c": 157} ] </textarea> <hr> <label>property name: </label> <input type="text" id="propertyName" value="b"/> <label>property value: </label> <input type="text" id="propertyValue" value=6 /> </fieldset> <hr> <button onclick="findObject()">find object in array!</button> <hr> <fieldset> <legend>Searching Result:</legend> <pre id="preview">click find</pre> </fieldset>

我不知道为什么你反对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;
      }
    }
  }
}
jsObjects.find(x => x.b === 6)

中数:

如果数组中的元素满足提供的测试函数,find()方法将返回数组中的一个值。否则返回undefined。


附注:像find()和箭头函数这样的方法不被旧的浏览器(如IE)所支持,所以如果你想支持这些浏览器,你应该使用Babel编译你的代码。

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

要访问第三个对象,使用:jsObjects[2]; 要访问第三个对象b的值,使用:jsObjects[2].b;

实现需求的方法:

使用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[我]); } }