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

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和bind将特定的键值传递给回调函数。

   function byValue(o) { 
       return o.a === this.a && o.b === this.b; 
   };   

   var result = jsObjects.find(byValue.bind({ a: 5, b: 6 }));

其他回答

你可以像下面这样使用箭头函数:

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}

使用find和bind将特定的键值传递给回调函数。

   function byValue(o) { 
       return o.a === this.a && o.b === this.b; 
   };   

   var result = jsObjects.find(byValue.bind({ a: 5, b: 6 }));

对象的过滤器数组,属性与值匹配,返回数组:

var result = jsObjects.filter(obj => {
  return obj.b === 6
})

请参阅Array.prototype.filter()上的MDN文档

const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ] let result = jsObjects。过滤器(obj => { 返回obj。B === 6 }) console.log(结果)

找到数组中第一个元素/对象的值,否则返回undefined。

var result = jsObjects.find(obj => {
  return obj.b === 6
})

请参阅Array.prototype.find()上的MDN文档

const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ] let result = jsObjects。Find (obj => { 返回obj。B === 6 }) console.log(结果)

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

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>

让这个答案的最佳/最快部分更具可重用性和清晰性:

function getElByPropVal(myArray, prop, val){
    for (var i = 0, length = myArray.length; i < length; i++) {
        if (myArray[i][prop] == val){
            return myArray[i];
        }
    }
}