Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript find object by property in array

// To find a specific object in an array of objects
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
Comment

find object in array javascript with property

let obj = objArray.find(obj => obj.id == 3);
Comment

find object in array by property javascript

// Find an object with a given property in an array
const desiredObject = myArray.find(element => element.prop === desiredValue);
Comment

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

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']
Comment

search an array of objects with specific object property value

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

Find an object in an array by one of its properties

const inventory = [
  {name: 'apples', quantity: 2},
  {name: 'bananas', quantity: 0},
  {name: 'cherries', quantity: 5}
];

function isCherries(fruit) {
  return fruit.name === 'cherries';
}

console.log(inventory.find(isCherries));
// { name: 'cherries', quantity: 5 }
Comment

Find object by any property

// Sample array
var myArray = [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Peter"},
    {"id": 3, "name": "Harry"}
];

// Get the Array item which matchs the id "2"
var result = myArray.find(item => item.id === 2);
console.log(result.name);  // Prints: Peter
// Get the index of Array item which matchs the id "2"
var index = myArray.findIndex(item => item.id === 2);
console.log(index);  // Prints: 1
console.log(myArray[index].name);  // Prints: Peter
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery window offset top 
Javascript :: how to call rest api with the useeffect hook in react 
Javascript :: how to preview a pdf document in react 
Javascript :: remove duplicates in json array based on two fields in lodash 
Javascript :: contains is not a function javascript 
Javascript :: bcd full form in electronics 
Javascript :: Remove duplication from array in javascript 
Javascript :: javascript array push middle 
Javascript :: firebase app named default already exists react native 
Javascript :: unique values in array javascript 
Javascript :: lodash remove element from array 
Javascript :: javascript contains substring 
Javascript :: js form check all required input 
Javascript :: Could not find the drag and drop manager in the context of ResourceEvents. Make sure to wrap the top-level component of your app with DragDropContext 
Javascript :: how to reload window in javascript 
Javascript :: text inside an image component react native 
Javascript :: download json file from s3 
Javascript :: jquery forEach is not a function 
Javascript :: moment js difference between two dates 
Javascript :: javascript regex .test 
Javascript :: js-cookie set expiration of cookie 
Javascript :: adding numbers in an array javascript 
Javascript :: mmap() failed: [12] Cannot allocate memory composer 
Javascript :: Javascript get sum of array values 
Javascript :: nesting in react js 
Javascript :: ajax done 
Javascript :: angular loop 
Javascript :: passing state in link react 
Javascript :: vue axios catch error 
Javascript :: get dir from filepath javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =