find in array typescript code example
Example 1: javascript find object by property in array
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
Example 2: typescript array find
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
Example 3: 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
});
}
Example 4: find value in array ts
this.persons = this.personService.getPersons().find(x => x.id == this.personId);