I have the following array of object :
var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{b: 2, a: 2},{d: 2, a: 2},{a: 2, b: 2}]
and I would like to sort each object by keys so that it will become:
var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{a: 2, b: 2},{a: 2, d: 2},{a: 2, b: 2}]
I have tried the following code:
//sort keys
wordSpecsArr.forEach((obj) => {
var ordered = {};
Object.keys(obj).sort().forEach((key) => {
ordered[key] = obj[key]
});
});
But it did not work. I would really appreciate any help