JSFiddle - React, Tailwind, and code Playground
by TiagoSalgado
JavaScript
if (!Array.prototype.getObjectFromPropertyValue) {
Array.prototype.getObjectFromPropertyValue = function (prop, value) {
for (var index = 0; index < this.length; index++) {
if (prop in this[index]) {
if (this[index][prop] == value) {
return this[index];
}
}
}
return null;
};
}
var arr = [{
id: 1,
text: 'stuff'
}, {
id: 2,
text: 'cool'
}];
console.log("using prototype");
console.log(arr.getObjectFromPropertyValue("id", '1'));
var result = $.grep(arr, function (e) {
return e.id == 1;
});
console.log("using jquery");
console.log(result);