0
How to transform the object?
Modify object of maps(k,v) to list of maps(v, [k])
4 Respuestas
+ 3
Kate
var exist = {
"33": "atutu",
"35": "atutu",
"42": "vilu",
"55": "vilu",
}
var abc = {}
for (var key in exist) {
if(abc[exist[key]])
abc[exist[key]].push(key)
else
abc[exist[key]] = [key]
}
console.log(JSON.stringify(abc))
+ 1
Thank you, A͢J !
0
Exist: {
"33": "atutu",
"35": "atutu",
"42": "vilu",
"55": "vilu",
}
To:
modify: [
"atutu": ["33", "35"],
"vilu": ["42", "55"]
]
0
const obj = {
"33": "atutu",
"35": "atutu",
"42": "vilu",
"55": "vilu",
}
const map1 = new Map(Object.entries(obj));
let map2 = new Map();
map1.forEach((v,k)=>
map2[v]?map2[v].push(k):map2[v]=[k]
);
console.log(JSON.stringify(map2))