Benassila Reda
Benassila Reda

Reputation: 33

map array inside an array Js

how can I map array inside ?

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

I tried

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

Upvotes: 2

Views: 82

Answers (1)

R4ncid
R4ncid

Reputation: 7129

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>)

Upvotes: 2

Related Questions