I have an object like this:
const object = {name: ['Jim', 'Jack', 'Betty'],
age: [14, 15, 16],
gender: ['M', 'M', 'F']}
What I want is to create a text like that:
Jim 14 M
Jack 15 M
Betty 16 F
Try using map()
const object = { name: ['Jim', 'Jack', 'Betty'], age: [14, 15, 16], gender: ['M', 'M', 'F'] } const transpose = (arr) => arr[0].map((_, colIndex) => arr.map(row => row[colIndex])); const transposed = transpose([object.name, object.age, object.gender]) const result = transposed.map(row => row.join('\\t')).join('\\n') console.log(result)
If you have a specific properties and have same length, this does the trick:
const object = {name: ['Jim', 'Jack', 'Betty'], age: [14, 15, 16], gender: ['M', 'M', 'F']} const [names,ages,genders] = Object.values(object); const rows = names.map((name, i)=>[name, ages[i], genders[i]]); const result = rows.map(row => row.join('\\t')).join('\\n') console.log(result)
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.