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

116
Vistas
React.JS Storing mapped API repsonse into a new array based on matching values

Using React, I have data from an API response. I mapped the data and am storing the visitID, which would be the main identifier, into a variable. What I would like to do is look for any matching visitID values and store them into a new array, the issue I'm having is each instance is being stored in it's own array, for example:

['7065682'] at Index 0

['7065682'] at Index 1

['7065682'] at Index 2

['7047674'] at Index 3

['7047674'] at Index 4

I would like to look through each iteration and check all, put matching values into it's own array so I can write some logic based off each unique value. I just don't understand how to look through the next iteration. Here's my code, and the function 'duplicateVisitID' that I've been trying, but it doesn't give me the results I'm looking for.

      {
        Object.keys(this.state.EncounterData).length !== 0 ?
          Object.values(this.state.EncounterData).map((encounter, i) => {
            

            const visitID = [encounter.resource.identifier[1].value];

            console.log(visitID, i);

            const duplicateVisitID = function (visitID) {
              if (visitID[i] === visitID[i])
              return [visitID.concat(visitID[i])]
            } 
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If I understand correctly, you want an array of unique values? If so then you can use the map function and add any unique values to an array if the value is not already in it:

const uniqueVals = [];

EncounteredData.map((visitID) => {
  if (!uniqueVals.includes(visitID[0])) {
    uniqueVals.push(visitID[0]);
  }
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

I am not sure what do you want to do, but if I understood right you want new array with only strings that are unique, not repeating. If yes see the code below. This is not the best performing one because of iteration inside iteration, but it will work for you. You can optimize it later by applying some algorithms.

The newArr is equal to ['7065682', '7047674']

const EncounteredData = [['7065682'], ['7065682'], ['7065682'], ['7047674'], ['7047674']];

const newArr = [];

for(let i of EncounteredData) {
    for(let j of EncounteredData) {
        if((i[0] !== j[0]) && !newArr.includes(i[0])) newArr.push(i[0]);
    }
}

console.log(newArr);
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