JSFiddle - React, Tailwind, and code Playground
JavaScript
var products = [{id:102, price:200.3},
{id:3, price:104.5},
{id:10, price:20}];
products.findById = function(id) {
for (var i=0, len=this.length; i<len; i++) {
if (id == this[i].id) return this[i];
}
return null;
};
products.sortById = function() {
this.sort(function(a,b) {
return a.id - b.id;
});
};
products.removeById = function(id) {
for (var i=0, len=this.length; i<len; i++) {
if (id == this[i].id) return this.splice(i, 1)[0];
}
return null;
};
console.log(products.findById(10));
products.sortById();
console.log(products);
products.push({id:123, price:200.49});
console.log(products.removeById(10));
console.log(products.removeById(20)); // non existent
console.log(products);