JSFiddle - React, Tailwind, and code Playground
by Ishank Dubey
JavaScript
function attachFindMethodToArray (theArray) {
if(!theArray || !theArray.length){
return undefined;
}
if(theArray.findTest){
return ;
}
if (!theArray.findTest) {
Object.defineProperty(theArray, 'findTest', {
value: function(predicate) {
var o = Object(theArray),
len = theArray.length,
thisArg = arguments[1],
k = 0;
console.log(thisArg, 'this');
while (k < len) {
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
k++;
}
return undefined;
}
});
}
};
var anArray = [{name:'test'}];
attachFindMethodToArray(anArray);
var result = anArray.findTest(function(ele){
return ele.name == 'testaaaa';
});
console.log(result);