I have a input array of objects
[
{ col1: 'col1value1', col2: 'col2value1', col3: col1value3},
{ col1: 'col1value2', col2: 'col2value2', col3: col1value2}
]
Now I want to convert this into a object two arrays like below
columns:["col1" , "col2","col3"],
data: ["col1value1","col2value1" , "col1value3"] ,
["col1value2","col2value2" , "col1value2"]
is there any good way or faster way. as I am trying to use 2 foreach loop on all the values and creating columns and data array
Use Object.keys()
method to get all the columns and use Array.prototype.map
method to traverse the array and get the values using Object.values()
method.
const input = [ { col1: 'col1value1', col2: 'col2value1', col3: 'col1value3' }, { col1: 'col1value2', col2: 'col2value2', col3: 'col1value2' }, ]; const columns = Object.keys(input[0]); const data = input.map((x) => Object.values(x)); console.log(columns); console.log(data);
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.