Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Mapping an array with two different styles = odd array with one style and even array in other style

This is the standard mapping for blog posts in Gatsby and they show all the blog posts with a title, the date, and the post.

const { blogs } = data.strapi
const { id } = data.strapi.blogs
  return (
    <Layout>
      <article>
        <header>
          <h1> {blogs.title} </h1>
          <p> {blog.description} </p>
        </header>
        <hr />
      </article>
    </Layout>
  )
};

export default BlogPost;

but how can I change the look that appears in the first blog post with the font to the right and color red; the second blog post title to the left and the color blue and so on?

I made an if conditional but I can't make it work


let even=blogs.filter((a,i)=>i%2===1); // 1 3 5
let odd = blogs.filter((a,i)=>i%2===0); // 0 2 4

          <div>
            {even.map((blog, i) => {
              return <h1 className="text-danger">{blog.title}</h1>
              })}
          </div>

// Output : title[0], title[2], title[4]; all red

//Expected Output: title[0] (red), title[1] (blue), title[2] (red), title[3] (blue) title[4] (red), title[5] (blue);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Assuming you're using CSS to style your blog, you can just conditionally add the class name to style each element at the same time you loop through them to print it.

      <div>
        {blogs.map((blog, i) => {
          return <h1 className={`text-danger ${i%2===1 ? 'isEven' : 'isOdd'}`}>{blog.title}</h1>
          })}
      </div>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!