0

I am trying to create some pills for a card display. I have an array of objects which I am mapping over to produce them on the page. Within the objects I have an array called tech which has something like tech: ['python', 'react.js'] etc.

something like this:

    const data = [
        {
          imgUrl: "https://images.unsplash.com/photo-1551882547-ff40c63fe5fa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
          description: 'oifonefpempfewpvpewmpew',
          title: "Some project name",
          tech: ["python", "react.js"],
        },
{
          imgUrl: "https://images.unsplash.com/photo-1551882547-ff40c63fe5fa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
          description: 'oifonefpempfewpvpewmpew',
          title: "Some project name",
          tech: ["python", "react.js"],
        },
    ]

I have mapped over that array like so.

<div className="">
  {data.map(tech => (
    <span className="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">
      {tech.tech}
    </span>
  ))}
</div>

Which it prints out the items, but it doesnt split them up into seperate pills just prints the array in one pill.

I am using Gatsby for my project

How can i split them up?

4
  • data.tech is the array you want to map over, but it doesn't seem to be an array of objects Commented Jul 6, 2020 at 16:07
  • the array is within an array of objects. Commented Jul 6, 2020 at 16:12
  • Are you sure you have posted the data correctly? There's no rating field at any level Commented Jul 6, 2020 at 16:13
  • there you go it should make sense now Commented Jul 6, 2020 at 16:14

2 Answers 2

1

You need to have another .map();

<div className="">
  {data.map(el => (
    el.tech.map(currTech => (
      <span className="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">
        {currTech}
      </span>
    ))        
  ))}
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

lovely...thanks for this. I was on the right lines before i got came here :)
Always remember, no of loops = no of arrays
0

From your code, you can see that you're just outputting the array at {tech.tech}: Try this, assuming you already created the component as Pill and data hold the array of project objects; I am also assuming this is a project component and the Pill component lives in it, then:

<div className="">
    {data.map(project => (
        <span className="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">
            {project.tech.map(t => <Pill tech={t} />)}
        </span>
    ))}
</div>

Comments

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.