JSFiddle - React, Tailwind, and code Playground
by painotpi
JavaScript
var json = {
"costA": 23122,
"costB": 241413,
"costC": 895454,
"costD": 8974
}
var sortJSON = function (data) {
var sJSON = '';
var aTemp = [];
$.each(data, function (i, d) {
aTemp.push(d);
});
aTemp.sort(function (a, b) {
return a - b;
});
aTemp.filter(function (item, index, arr) {
return index === arr.indexOf(item);
});
$.each(aTemp, function (i, d) {
$.each(data, function (_i, _d) {
if (aTemp[i] === data[_i]) {
sJSON += '"' + _i + '"' + ":" + _d + ",";
}
});
});
sJSON = sJSON.replace(/,$/gi, "");
sJSON = "{" + sJSON + "}";
console.log(aTemp, JSON.parse(sJSON));
}
sortJSON(json);