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

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


当前回答

好吧,有几种方法可以做到这一点,但让我们从最简单也是最新的方法开始,这个函数叫做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}]

其他回答

使用underscore.js:

var foundObject = _.findWhere(jsObjects, {b: 6});
jsObjects.find(x => x.b === 6)

中数:

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


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

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

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>

实现需求的方法:

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

如果我理解正确的话,你想在数组中找到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;
});