Ask AI on The Internet
Ask Another Question Browse Other QuestionsQuestion: John [ { id: 0, key: 'City', value: 'Texas', preference: 0 }, { id: 0, key: 'Phone number', value: '456', preference: 0 }, { id: 0, key: 'address', value: 'Bangalore', preference: 0 } ] admin [] Steve [ { id: 0, key: 'City', value: 'ND', preference: 0 }, { id: 0, key: 'Phone number', value: '789', preference: 0 } ] how to get c=[John,Texas,456,Bangalore], d=[admin] and e=[Steve,ND,789]
?</code> I want to get c=[John,Texas,456,Bangalore], d=[admin] and e=[Steve,ND,789] from the above data. I tried with for of loop and if condition but it not worked. Can anyone help me how can i achieve this? Thanks A: You can use <code>.map()</code> and spread operator <code>...</code> to format data in the way you want <code>const data = { John: [{ id: 0, key: 'City', value: 'Texas', preference: 0 }, { id: 0, key: 'Phone number', value: '456', preference: 0 }, { id: 0, key: 'address', value: 'Bangalore', preference: 0 } ], admin: [], Steve: [{ id: 0, key: 'City', value: 'ND', preference: 0 }, { id: 0, key: 'Phone number', value: '789', preference: 0 } ] } let c = [] c.push(Object.keys(data)[0], ...Object.values(data)[0].map(o => o.value)) let d = [] d.push(Object.keys(data)[1]) let e = [] e.push(Object.keys(data)[2], ...Object.values(data)[2].map(o => o.value)) console.log('c: ', c) console.log('d: ', d) console.log('e: ', e)</code>
Jan. 25, 2023, 7:09 a.m.
Think your answer is better than the AI's? Post it below.
Question Tags
If you want your question answered by an AI, click here.