xxxxxxxxxx
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']
xxxxxxxxxx
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"}
xxxxxxxxxx
// To find a specific object in an array of objects
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
xxxxxxxxxx
// Find an object with a given property in an array
const desiredObject = myArray.find(element => element.prop === desiredValue);
xxxxxxxxxx
// 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
});
xxxxxxxxxx
function variableName(value) {
for (var i=0, iLen=array.length; i<iLen; i++){
if (array[i].property == value) return array[i]
}
};