I have an array of objects each containing between one and more items. how to create a new object array where each item would take the key of the previous array? I tried to extract the key with object.keys and code something but for more than 2 hours I am completely stuck, I am not achieving the expected result.
What methods could I use?
Edit, the last thing I tried :
Object.entries(categories).forEach(([key, value]) => { const k = Object.keys(categories).find(key => categories[key] === value); array.push({ [k]: value }); });
INPUT
[ { "shopCategory": { "shop1": "object" } }, { "placeCategory": { "place1": "object", "place2": "object" } }, { "sellerCategory": { "seller1": "object", "seller2": "object", "seller3": "object" } } ]
OUTPUT
[ { "shopCategory": { "shop1": "object" } }, { "placeCategory": { "place1": "object" } }, { "placeCategory": { "place2": "object" } }, { "sellerCategory": { "seller1": "object" } }, { "sellerCategory": { "seller2": "object" } }, { "sellerCategory": { "seller3": "object" } } ]
https://stackoverflow.com/questions/65397904/from-an-array-of-objects-how-to-create-a-new-object-array-where-each-item-would December 22, 2020 at 02:12AM
没有评论:
发表评论