简体   繁体   中英

Node JS array formation

I am pulling data from mysql and my node js api is responding the complete row as an array . but the front end developer needs the first column out of the main array .

Current api response :

[{
  column1 :data,
  column2 :data,
  column3 :data
}] 

Front end developer needs :

[{ column 1 : data },
 { column2 : data, column3 : data }]

I am sending the data back as res.send(rows) currently.

Believe I need to use a for condition.

Can someone help?

OK, the first code here was a joke

This should work fine

 const indata = [{ column1 :'data1', column2 :'data2', column3 :'data3' },{ column1 :'data4', column2 :'data5', column3 :'data6' }]; const outdata = [].concat(...indata.map(item => Object.entries(item).map(([key, data]) => ({[key]: data})))); console.log(outdata); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM