Home > OS >  Need help for map over multiple nested arrays
Need help for map over multiple nested arrays

Time:08-01

After reading and trying everything I could, I decided to ask you for help! I just can't handle it...

I have blog posts that looks like this:

const posts = [
{
  section: 'one',
  title: [ 'third', 'fourth' ],
  description: [ 'third', 'fourth' ],
  slug: [ '/third', '/fourth' ]
},
{
  section: 'three',
  title: [ 'second' ],
  description: [ 'second' ],
  slug: [ '/second' ]
},
{
  section: 'two',
  title: [ 'first', 'fifth' ],
  description: [ 'first', 'fifth' ],
  slug: [ '/first', '/fifth' ]
}

]

The goal is to render them in a react component most likely and look like this:

<div>
    <h2>one</h2>
    <div>
      <h3>third</h3>
      <p>third</p>
      <a href="#">/third</a>
    </div>
    <div>
      <h3>fourth</h3>
      <p>fourth</p>
      <a href="#">/fourth</a>
    </div>
  </div>

Just proper render of posts inside corresponding sections. They will be more. This is just an example :)

The best I've come up with (and it doesn't work for me) is this:

{posts.map(x =>
    <div key={x.section}>
      <h2 key={x.section}>{x.section}</h2>
      <div>
        <h3>{x.title}</h3>
        <p>{x.description}</p>
        <a href={x.slug}>{x.slug}</a>          
      </div>
    </div>
   )} 

Thank you in advance !!!

CodePudding user response:

You can nest maps like so:

{posts.map(x =>
    <div key={x.section}>
      <h2>{x.section}</h2>
      {
        x.title.map((title, idx) => 
          <div key={idx}>
            <h3>{title}</h3>
            <p>{x.description[idx]}</p>
            <a href={x.slug[idx]}>{x.slug[idx]}</a>          
          </div>
      }
    </div>
)}

This only works if we can make the assumption that x.title, .section, and .description have the same dimension.

CodePudding user response:

const posts = [
  {
    section: 'one',
    title: [ 'third', 'fourth' ],
    description: [ 'third', 'fourth' ],
    slug: [ '/third', '/fourth' ]
  },
  {
    section: 'three',
    title: [ 'second' ],
    description: [ 'second' ],
    slug: [ '/second' ]
  },
  {
    section: 'two',
    title: [ 'first', 'fifth' ],
    description: [ 'first', 'fifth' ],
    slug: [ '/first', '/fifth' ]
  }
]

class App extends React.Component {
  render() {
    return (
      <div>
        {posts.map((post, index) => {
          return (
            <div>
              <h2>{post.section}</h2>
              {post.title.map((title, index) => {
                return (
                  <div>
                    <h3>{title}</h3>
                    <p>{post.description[index]}</p>
                    <a href={post.slug[index]}>{post.slug[index]}</a>
                  </div>
                )
              })}
            </div>
          )
        })}
      </div>
    )
  }
}

CodePudding user response:

Assuming that the array has the same length and order

{posts.map(x =>
  <div key={x.section}>
    <h2 key={x.section}>{x.section}</h2>
    {x.title.map((_, i) => (
      <div>
        <h3>{x.title[i]}</h3>
        <p>{x.description[i]}</p>
        <a href={x.slug[i]}>{x.slug[i]}</a>          
      </div>
    ))}
  </div>
)} 

CodePudding user response:

Try this:

export default function App() {
  const posts = [
    {
      section: "one",
      title: ["third", "fourth"],
      description: ["third", "fourth"],
      slug: ["/third", "/fourth"]
    },
    {
      section: "three",
      title: ["second"],
      description: ["second"],
      slug: ["/second"]
    },
    {
      section: "two",
      title: ["first", "fifth"],
      description: ["first", "fifth"],
      slug: ["/first", "/fifth"]
    }
  ];

  return (
      <div>
        {posts.map((post) => {
          return (
            <>
              <h2>{post.section}</h2>
              {post.title.map((item, index) => {
                return (
                  <div>
                    <h3>{item}</h3>
                    <p>{post.description[index]}</p>
                    <a href="#">{post.slug[index]}</a>
                  </div>
                );
              })}
            </>
          );
        })}
      </div>
  );
}

Basically, this maps over each post item and then for each post, maps over title array and adds the corresponding title, description and slug.

Note: This assumes that the length of title, description and slug lists will be equal.

  • Related