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

227
Vistas
find FIRST occurrence of element from Array1 IN Array2, Take the Index that element is found at in Array2, and make a new Array

I have two arrays. look by id, find FIRST occurrence of element from Array1 IN Array2, Take the Index that element is found at in Array2, and make a new Array where element in Array1 is moved to the new index found in Array2. in Array1 {id: 001} is at index 0, I would like it to be in a new Array at index 1 (index it is FIRST found in Array2). {id: 002} is at index 1 in Array1, I would like this to be in the new Array at index 3( index it is FIRST found in Array2) so on....

const Array1 = [
    {
      id: '001',
      school: "blue springs"
    },
    {
      id: '002',
      school: "sycamore hills"
    },
    {
      id: '003',
      school: "moreland ridge"
    },
    {
      id: '004',
      school: "grain valley"
    }
]


const Array2 = [
         {
      id: '003',
      participant: "Susan"
    },
    {
      id: '001',
      participant: "Henry"
    },
    {
      id: '003',
      participant: "Justin" <---- if 003 exists do not duplicate the return array, this is my issue....
    },
    {
      id: '004',
      participant: "Jessica"
    },
    {
      id: '002',
      participant: "Carly"
    },
    {
      id: '001',
      participant: "Chloe"  <---- if 001 exists do not duplicate the return array, this is my issue....
    }

// got this far, 

const finder = Array1.map((el) => { return Array2.findIndex((el2) => { return el2.id === el.id}) })


console.log(finder)
// [ 1, 4, 0, 3 ] <--- need to move Array1 objects to these new indexes 

expected output

const Result = [
    {
      id: '003',
      school: "moreland ridge"
    },
    {
      id: '001',
      school: "blue springs"
    },
    {
      id: '004',
      school: "grain valley"
    },
    {
      id: '002',
      school: "sycamore hills"
    }
]

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You first filter to remove duplicates on Array2, and then look for a match in Array1 regarding id's

const Array1 = [
    {
      id: '001',
      school: "blue springs"
    },
    {
      id: '002',
      school: "sycamore hills"
    },
    {
      id: '003',
      school: "moreland ridge"
    },
    {
      id: '004',
      school: "grain valley"
    }
]


const Array2 = [
         {
      id: '003',
      participant: "Susan"
    },
    {
      id: '001',
      participant: "Henry"
    },
    {
      id: '003',
      participant: "Justin" // <---- if 003 exists do not duplicate the return array, this is my issue....
    },
    {
      id: '004',
      participant: "Jessica"
    },
    {
      id: '002',
      participant: "Carly"
    },
    {
      id: '001',
      participant: "Chloe" // <---- if 001 exists do not duplicate the return array, this is my issue....
    }
]
const alreadyShown = {};
const res = Array2.filter( el => {
  if (!alreadyShown[el.id]){
    alreadyShown[el.id] = true;
    return true;
  }
  return false;
}).map( x => Array1.find( y => x.id === y.id ) || x);


console.log(res)

    

NOTE: I included a logical OR to provide a fallback case, but this is not defined in OP request

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you just need to sort the first array by the uniqueness of the identifier in the second array

const Array1 = [{id: '001',school: "blue springs"},{id: '002',school: "sycamore hills"},{id: '003',school: "moreland ridge"},{id: '004',school: "grain valley"}];

const Array2 = [{id: '003',participant: "Susan"},{id: '001',participant: "Henry"},{id: '003',participant: "Justin"},{id: '004',participant: "Jessica"},{id: '002',participant: "Carly"},{id: '001',participant: "Chloe" }];

const idOrder = Array2.map(({ id }) => id).filter((v, i, a) => a.indexOf(v)===i);
console.log('Order:', idOrder );

const sortedByOrder = Array1.sort(({ id: id1 }, { id: id2 }) => 
  idOrder.indexOf(id1) - idOrder.indexOf(id2));

console.log('Result:', sortedByOrder);
.as-console-wrapper{min-height: 100%!important; top: 0}

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