我有一个具有此结构{aaas:"good","aasda":"ok","adasa":"good","ascas":"good","asdasd":"nw","asdasda":"nw",asdqw:"nw","assa":"ok","asscsa":"ok"}的对象

在这里,我想将具有相似值的每个键分组,然后将其放入新对象。
就像newObj={"aaas":"good","adasa":"good","ascas":"good"}

最佳答案

您可以使用对象并按值分组。



var object = { aaas: "good", aasda: "ok", adasa: "good", ascas: "good", asdasd: "nw", asdasda: "nw", asdqw: "nw", assa: "ok", asscsa: "ok" },
    grouped = {};

Object.keys(object).forEach(function (k) {
    if (!grouped[object[k]]) {
        grouped[object[k]] = {};
    }
    grouped[object[k]][k] = object[k];
});

console.log(grouped);

.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 如何在angularjs中按值分组对象键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40339110/

10-11 14:46