Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

439
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda