JSFiddle - React, Tailwind, and code Playground
by manu v
JavaScript
const data = [{"name":"Jason","position":"CEO"},{"name":"Jason","position":"CEO"},{"name":"Jason","position":"CEO"},{"name":"Jason","position":"CEO"},{"name":"Jason","position":"CEO"}];
function genHash(str) {
var hash = 0, i, chr;
if (str.length === 0) return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
return hash;
};
// For each object in the array create
// a stringyfied version of it and create
// a hash
data.forEach(obj => {
obj.id = genHash(JSON.stringify(obj));
});
console.log(data);