gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
Please use JavaScript I have…
- User
Please use JavaScript
I have data:[{'001',{111:123,113:321,134:654,135:456,136:1554,101:56800,109:0,115:0}},{'006',{111:113,113:3211,134:154,135:156,136:154,101:5800,109:10,115:1000}}]
I need formatting data: [{mname:001,111:123,113:321,134:654,135:456,136:1554,101:56800,109:0,115:0},{mname:006,111:113,113:3211,134:154,135:156,136:154,101:5800,109:10,115:1000}]
- Assistant
You can use the
mapfunction to iterate over the elements in thedataarray and create a new object with the desired format. Here's how you can do it using JavaScript:const data = [ { "001": { 111: 123, 113: 321, 134: 654, 135: 456, 136: 1554, 101: 56800, 109: 0, 115: 0 } }, { "006": { 111: 113, 113: 3211, 134: 154, 135: 156, 136: 154, 101: 5800, 109: 10, 115: 1000 } }, ]; const formattedData = data.map((item) => { const key = Object.keys(item)[0]; const values = item[key]; return { mname: key, ...values }; }); console.log(formattedData);This should output:
[ { mname: "001", 111: 123, 113: 321, 134: 654, 135: 456, 136: 1554, 101: 56800, 109: 0, 115: 0 }, { mname: "006", 111: 113, 113: 3211, 134: 154, 135: 156, 136: 154, 101: 5800, 109: 10, 115: 1000 }, ]