Example 1: javascript find
const inventory = [
{name: 'apples', quantity: 2},
{name: 'cherries', quantity: 8}
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
{name: 'cherries', quantity: 15}
];
const result = inventory.find( ({ name }) => name === 'cherries' );
console.log(result)
Example 2: javascript array.find
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
Example 3: array find
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
Example 4: find method javascript
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));
Example 5: find all of array which satisfy condition javascript
myArray.filter(x => x > 5)
Example 6: javascript array find
var myArrayOfAges = [1,4,6,8,9,13,16,21,53,78];
var result = myArrayOfAges.find(age => age >= 12);
console.log(result);
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
value: function(predicate) {
if (this == null) {
throw TypeError('"this" is null or not defined');
}
var o = Object(this);
var len = o.length >>> 0;
if (typeof predicate !== 'function') {
throw TypeError('predicate must be a function');
}
var thisArg = arguments[1];
var k = 0;
while (k < len) {
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
k++;
}
return undefined;
},
configurable: true,
writable: true
});
}