简体   繁体   中英

check array of objects and compare its object values

I have a simple array of objects and I'd like to check if all the object in that array has the same value for a specific property.

const arr = [{
    name: "Test",
    value: 90
}, {
    name: "OP",
    value: 90
}, {
    name: "Test",
    value: 120
}]

Here I want to compare all the value property of every object element.

// get first objects value for comparison
let same_value = arr[0]['value'];

for(let i = 0; i < arr.length; i ++) {
  if (arr[i]['value'] != same_value) {
    return false;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM