DekGenius.com
JAVASCRIPT
js get array item by property
const jsObjects = [
{id: 1, displayName: "First"},
{id: 2, displayName: "Second"},
{id: 3, displayName: "Third"},
{id: 4, displayName: "Fourth"}
]
// You can use the arrow function expression:
var result = jsObjects.find(obj => {
// Returns the object where
// the given property has some value
return obj.id === 1
})
console.log(result)
// Output: {id: 1, displayName: "First"}
javascript find object by property in array
// To find a specific object in an array of objects
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
find object in array javascript with property
let obj = objArray.find(obj => obj.id == 3);
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);
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
});
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']
search an array of objects with specific object property value
var result = jsObjects.find(obj => {
return obj.b === 6
})
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]
}
};
© 2022 Copyright:
DekGenius.com