0

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
0

2 Answers 2

5

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.