1

This is my object {angry: "ok", sad: "nw", fire: "ok", happy: "nw","something":"good"}

I want to change this object to something like this:

val[nw][0]="sad"
val[nw][1]="happy"
val[ok][0]="angry"
val[ok][1]="fire"
val[good][0]="something"

how can i do that?

1 Answer 1

2

You could iterate the keys and build a new object upon.

var object = { angry: "ok", sad: "nw", fire: "ok", happy: "nw", something: "good" },
    result = {};

Object.keys(object).forEach(function (k) {
    result[object[k]] = result[object[k]] || [];
    result[object[k]].push(k);
});

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.