I have this variable:
let json1 =
{'aaa': {'cus1':1,'cus2':2},
'bbb': {'cus3':1,'cus4':5}
}
And I would like to convert it into the following array:
[{'aaa': {'cus1':1,'cus2':2}},
{'bbb': {'cus3':1,'cus4':5}}
]
What I tried to do is:
let arr = [];
let keys = Object.keys(json1);
keys.reduce((acc, key) => {
acc.push({key: json1[key]});
return acc;
}, arr);
While I get:
[ { key: { cus1: 1, cus2: 2 } }, { key: { cus3: 1, cus4: 5 } } ]
So evidently I would like to use the true key instead of key
as the key of my encapsulated json in the arr.
P.S. Is there any way to do this without using for loop?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…