user2786092
user2786092

Reputation: 79

Repeat an array object with key and values with map in react js

Hi i want to repeat an array object including object keys(once) and all the object values with map function

[
   { name: 'abc', age:'1' },
   { name: 'def', age:'2' },
   { name: 'ghi', age:'3' }
]

and i want repeat like this

name  age
abc    1
def    2
ghi    3

thanks

Upvotes: 1

Views: 986

Answers (1)

raj m
raj m

Reputation: 2023

 let data = [
   { name: 'abc', age:'1' },
   { name: 'def', age:'2' },
   { name: 'ghi', age:'3' }
  ]

  rows = [] ; for (i in data[0]) {rows.push(i)}

Here you can display rows using "rows" array Below code is to display columns

   data.map(item => {rows.map(row => {console.log(item[row])})})

Upvotes: 1

Related Questions