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

438
Views
How to not show the duplicate if it exists already?

I want to make an alphabet index for businesses, and struggling to eliminate the duplicate. Like below:

T
  Toy Store 1
  Toy Store 2
  Toy Store 3

So I sliced the initial letter of the business and want to delete it if the letter is already shown like below:

T
  Toy Store 1
T // <- want to delete
  Toy Store 2
T // <- want to delete
  Toy Store 3

I tried using new Set but it does not work. I also tried using indexOf and filter method but didn't work either. Appreciate any help.

Component ListItem

export default function CatListItem(props) {
  const {image,title,slicedInitial,onPress} = props;
    return (
      <View>
            <Text>
              {slicedInitial}
            </Text>
            <Text>
              {title}
            </Text>
      </View>
    );
  };

Screen:

const renderList = () => {

//does not work
const indexedSlicedInitial=(item)=>{
  const slicedInital=item.title.slice(0,1)
  const index = [...new Set(slicedInital)]
  return index
}
//
    return (
      <View>
        {
          <Animated.FlatList
            data={data.list}
            key={"list"}
            keyExtractor={(item, index) => `list ${index}`}
            renderItem={({ item, index }) => (
              <ListItem
                list
                title={item.title}
                slicedInitial={() => indexedSlicedInitial(item)}
                onPress={() => onProdDetail(item)}
              />
          />
    );
  };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use state in screen component to store prevIndex so, that you can compare everytime you get newIndex.

If they are not same then only return new index otherwise return empty string. On ListItem component render text only if its not an empty string.

Screen:

// use state to store prev value
const [prevIndex, setPrevIndex] = useState('')

const renderList = () => {

const indexedSlicedInitial=(item)=>{
  const slicedInital=item.title.slice(0,1)
  
  // if they are different then only return new index
  if(prevIndex !== slicedInitial) {
    setPrevIndex(slicedInitial)
    return slicedInitial
  }

  // if slicedInitial same as prevIndex
  return "";
}

// no changes below

Component ListItem:

export default function CatListItem(props) {
  const {image,title,slicedInitial,onPress} = props;
  return (
    <View>
      // only output slicedInitial if not empty
      {slicedInitial ? <Text>
        {slicedInitial}
      </Text> : null}
      <Text>
        {title}
      </Text>
    </View>
  );
};
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!