Home > database >  map array inside an array Js
map array inside an array Js

Time:05-24

how can I map array inside ?

array = [[{},{},{}],[{},{}],[{},],[{},{},{},{}]]

I tried

data.map((array) => {
            console.log(array);
            return (
                array.map((doc) => <p>{doc}</p>)
            );
        })

CodePudding user response:

you can use flat and then map like this

const data =  [[{value: 1},{value: 2},{value: 3}],[{value: 4},{value: 5}],[{value: 6}],[{value: 7},{value: 8},{value: 9},{value: 10}]]


const html = data.flat().map(d => `<p>${d.value}</p>`).join('')

const htmlReduce = data.reduce((res, item) => [...res, ...item], []).map(d => `<p>${d.value}</p>`).join('')

console.log(html, htmlReduce)

based on your code

data.flat().map((doc, i) => <p key={i}>{doc}</p>)

for old browsers

data.reduce((res, item) => [...res, ...item], []).map((doc, i) => <p key={i}>{doc}</p>)
  • Related