2

how can I map array inside ?

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

I tried

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

1 Answer 1

2

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>)
1
  • The flat method is not yet implemented in common browsers (only Chrome v69, Firefox Nightly and Opera 56). It’s an experimental feature, is there's another way to do it ? Commented May 23, 2022 at 17:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.