Get JavaScript object from object array by property value [duplicate]

本文翻译自:Get JavaScript object from array of objects by value of property [duplicate]

This question already has an answer here: This question already has an answer here :

Let's say I have an array of four objects: Suppose I have an array of four objects :

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

A Way the I that there IS CAN GET The THIRD Object ( {a: 5, b: 6}) by The Property The value of bfor the without Example A for...inLoop? There may be a method by attribute bobtaining a third object value ( {a: 5, b: 6}), e.g., no for...inloop?


#1st Floor

Reference: https://stackoom.com/question/waiJ/ Get JavaScript object from object array by property value-repeat


#2nd Floor

Filterarray of objects, which property matches value, returns array: array of objects, Filterwhose properties match the value, return array:

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

At The See MDN Docs ON Array.prototype.filter () See Array.prototype.filter () on the MDN documentation

 const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ] let result = jsObjects.filter(obj => { return obj.b === 6 }) console.log(result) 

FindThe value of First Element The / The Object in Array, otherwise undefinedIS returned. FindThe first element / value of an object in the array, otherwise undefined.

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

At The See MDN Docs ON Array.prototype.find () See Array.prototype.find () on the MDN documentation

 const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ] let result = jsObjects.find(obj => { return obj.b === 6 }) console.log(result) 


#3rd floor

I understand correctly & the I IF, you want to at The Object in the Find at The Array Whose bProperty IS 6? If I understand correctly, you want the bproperty to 6find objects in the array.

var found;
jsObjects.some(function (obj) {
  if (obj.b === 6) {
    found = obj;
    return true;
  }
});

Or if you were using underscore: Or, if you used underscore:

var found = _.select(jsObjects, function (obj) {
  return obj.b === 6;
});

#4th floor

I do not know why you are against a for loop (presumably you meant a for loop, not specifically for..in), they are fast and easy to read. I do not know why you are opposed to the for loop (probably refers to the for loop , Not specifically for ..in), they are fast and easy to read. Anyhow, here's some options. Anyway, here are some options.

For loop: For loop:

function getByValue(arr, value) {

  for (var i=0, iLen=arr.length; i<iLen; i++) {

    if (arr[i].b == value) return arr[i];
  }
}

.filter . filter

function getByValue2(arr, value) {

  var result  = arr.filter(function(o){return o.b == value;} );

  return result? result[0] : null; // or undefined

}

.forEach .for each

function getByValue3(arr, value) {

  var result = [];

  arr.forEach(function(o){if (o.b == value) result.push(o);} );

  return result? result[0] : null; // or undefined

}

If, on the other hand you really did mean for .. in and want to find an object with any property with a value of 6, then you must use for .. unless unless you pass the names to check. On the other hand, if You really do want to use ..in and want to find an object with any attribute with a value of 6, then unless you check by name, you must use for..in eg for example

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;
      }
    }
  }
}

#5th Floor

Looks like in at The ECMAScript IT 6 Proposal there are at The ArrayMethods find()and findIndex(). Looks in ECMAScript 6 proposal seems to have a Arraymethod find()and findIndex(). MDN also offers polyfills which you can include to get the functionality of these across all browsers. MDN also offers polyfills, which you can include to get its functionality in all browsers.

find() : 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() : 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

#6th floor

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

to access the third object, use: jsObjects[2]; To access the third object, use: jsObjects[2];
to Object Access at The THIRD, b value, use: jsObjects[2].b; To access the third object b values, use:jsObjects[2].b;

Published 0 original articles · praised 8 · 30,000+ views

Guess you like

Origin blog.csdn.net/asdfgh0077/article/details/105508765