0

How to transform the object?

Modify object of maps(k,v) to list of maps(v, [k])

11th Jun 2022, 1:25 PM
Kate
4 Answers
+ 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))
11th Jun 2022, 1:58 PM
A͢J
A͢J - avatar
+ 1
Thank you, A͢J !
11th Jun 2022, 3:57 PM
Kate
0
Exist: { "33": "atutu", "35": "atutu", "42": "vilu", "55": "vilu", } To: modify: [ "atutu": ["33", "35"], "vilu": ["42", "55"] ]
11th Jun 2022, 1:31 PM
Kate
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))
11th Jun 2022, 8:25 PM
Solo
Solo - avatar