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

79
Views
apply style when 2 adjacent items have different attribute

I have an array of objects like:

{"code": 123, "description": "Item 1", "group": 2},
{"code": 211, "description": "Item 2", "group": 2},
{"code": 234, "description": "Item 3", "group": 3},
{"code": 255, "description": "Item 4", "group": 4},
{"code": 311, "description": "Item 5", "group": 4},

I have a FlatList

<FlatList
data={products}
...
renderItem={({item, i }) => {
return <SingleItem key={item.code} } product={item} />
}}
/>

I rendered the list with FlatList and I want to apply a style to a <SingleIem> if 2 adjacent items have different groups.

e.g.

1) Items 1 & 2 have the same group -> no style will be applied.
2) Items 2 & 3 have different groups -> style will be applied to Item 3
3) Items 3 & 4 have different groups -> style will be applied to Item 4

and so on.

How can I achieve this?

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

0

If you already have an array of objects at hand and an index that means that you can compare the current index to the last index and check if the group property is the same for both. Place inside of the renderItem function and assign classes accordingly

renderItem={ (item, i) => {
      const isSameCategrory = arr[i]?.group === arr[i-1]?.group;
      // rest of code goes here
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

You can compare the group property using the i argument

const arr = [
    {"code": 123, "description": "Item 1", "group": 2},
    {"code": 211, "description": "Item 2", "group": 2},
    {"code": 234, "description": "Item 3", "group": 3},
    {"code": 255, "description": "Item 4", "group": 4},
    {"code": 311, "description": "Item 5", "group": 4}    
]

renderItem={({item, i }) => {

    const isDifferentGroup = arrOfObjects[i]?.group !== arrOfObjects[i-1]?.group;

    return ( 
        <div className={isDifferentGroup ? "class-name" : undefined}>
            <SingleItem key={item.code} } product={item} />
        </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!