Fellow Stranger Fellow Stranger - 2 months ago 14
React JSX Question

How to use Lodash to create nested divs

With the following code:

_.chunk(_.range(4), 2)


I get these nested arrays:

[[0, 1], [2, 3]]


How could I use the Lodash method
_.chunk()
to create something like this:

<div>
<div>1</div>
<div>2</div>
</div>
<div>
<div>3</div>
<div>4</div>
</div>

Answer

You can do it like this for example

    const arr = _.chunk(_.range(4), 2);
    return (
      <div>
        {arr.map(sub =>
          <div>
            {sub.map(elem =>
              <div>{elem + 1}</div>
            )}
          </div>
        )}
      </div>
    );