JSFiddle - React, Tailwind, and code Playground
JavaScript
var IndexedArray = function(key) {
this.key = key || 'id';
this.data = [];
this.index = {};
};
IndexedArray.prototype.addOrReplace = function(object) {
var id = object[this.key],
index = this.index[id];
if(index === undefined) {
index = this.data.length;
this.index[id] = index;
}
this.data[index] = object;
};
var indexedArray = new IndexedArray('uid');
indexedArray.addOrReplace({uid: 1, name: "bla", description: "cucu"});
indexedArray.addOrReplace({uid: 2, name: "smth else", description: "cucarecu"});
indexedArray.addOrReplace({uid: 1, name: "bli", description: "cici"});
console.log(indexedArray.data);