Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

441
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda