2

how can I map array inside ?

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

I tried

data.map((array) => {
            console.log(array);
            return (
                array.map((doc) => <p>{doc}</p>)
            );
        })
7
  • And what happened? What output are you expecting? Commented May 23, 2022 at 17:03
  • do you want to flat and map? Commented May 23, 2022 at 17:07
  • an "id" but it looks like I can't 2 map in JSX Commented May 23, 2022 at 17:08
  • actually you can Commented May 23, 2022 at 17:10
  • 1
    stackoverflow.com/questions/43756283/… Commented May 23, 2022 at 17:11

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>)
Sign up to request clarification or add additional context in comments.

1 Comment

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 ?

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.