Answers for "javascript find all objects in array by property value"

4

get array of all property in object array

objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ];
let result = objArray.map(a => a.foo); //[1,3,5]
//OR
let result = objArray.map(({ foo }) => foo) //[1,3,5]
Posted by: Guest on April-20-2021
2

javascript find object in array by property value

const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];

const filterItems = (needle, heystack) => {
  let query = needle.toLowerCase();
  return heystack.filter(item => item.toLowerCase().indexOf(query) >= 0);
}

console.log(filterItems('ap', fruits)); // ['apple', 'grapes']
console.log(filterItems('ang', fruits)); // ['mango', 'orange']
Posted by: Guest on December-11-2020
2

search an array of objects with specific object property value

// MDN Ref:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

var result = jsObjects.find(obj => {
  return obj.b === 6
});
Posted by: Guest on May-25-2020
0

return object from array by property value

function variableName(value) {
    for (var i=0, iLen=array.length; i<iLen; i++){
        if (array[i].property == value) return array[i]
    }
};
Posted by: Guest on May-23-2022

Code answers related to "javascript find all objects in array by property value"

Code answers related to "Javascript"

Browse Popular Code Answers by Language