Suraz Khanal Suraz Khanal - 10 months ago 35
Javascript Question

How to group object key by value in angularjs

I have an object with this structure


Here I want to group each key with the similar value and put these to new object.
Just like

Answer Source

You could use an object and group by value.

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];

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