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

86
Vistas
map unequal length of array of object

I have two arrays of object data that is the unequal length I just want to create a third array that is given below

first input array data

let firstArray = [{
    id: '0',
    name: 'shanu',
    age: '21'
  },
  {
    id: '1',
    name: 'bhanu',
    age: '21'
  },
  {
    id: '2',
    name: 'chary',
    age: '21'
  },
]

second set of array of data

let secondArray = [{
    id: '10',
    name: 'shanu',
    age: '28'
  },
  {
    id: '11',
    name: 'raanu',
    age: '29'
  },
  {
    id: '12',
    name: 'chary',
    age: '30'
  },
  {
    id: '15',
    name: 'kushal',
    age: '31'
  },
]

output array- if second set name watch with first set of data replace the second set id with first set it

let outputArray = [{
    id: '0',
    name: 'shanu',
    age: '28'
  },
  {
    id: '11',
    name: 'raanu',
    age: '29'
  },
  {
    id: '2',
    name: 'chary',
    age: '30'
  },
  {
    id: '15',
    name: 'kushal',
    age: '31'
  },
]

I'm not able to figure out how to map and filter together two different length of carry of array of object

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

0

You could map the secondArray and replace the id if name is found in firstArray:

let firstArray = [
  { id: '0', name: 'shanu', age: '21' },
  { id: '1', name: 'bhanu', age: '21' },
  { id: '2', name: 'chary', age: '21' }
]

let secondArray = [
  { id: '10', name: 'shanu', age: '28' },
  { id: '11', name: 'raanu', age: '29' },
  { id: '12', name: 'chary', age: '30' },
  { id: '15', name: 'kushal', age: '31' }
]

let thirdArray = secondArray.map((item) => {
  let found = firstArray.find((i) => i.name === item.name)
  return { ...item, id: found ? found.id : item.id }
})

console.log(thirdArray)

about 4 years ago · Juan Pablo Isaza Denunciar

0

The below may be one method to achieve the desired objective:

Code Snippet

const updateId = (arr1, arr2) => ( // arr1 has the up-to-date / current "id"
  arr2.map(({name, ...rest}) => (
    arr1.some(ob => ob.name === name)
    ? {name, ...rest, id: arr1.find(ob => ob.name === name)?.id}
    : {name, ...rest}
  ))
);

const firstArray = [{
    id: '0',
    name: 'shanu',
    age: '21'
  },
  {
    id: '1',
    name: 'bhanu',
    age: '21'
  },
  {
    id: '2',
    name: 'chary',
    age: '21'
  },
]

const secondArray = [{
    id: '10',
    name: 'shanu',
    age: '28'
  },
  {
    id: '11',
    name: 'raanu',
    age: '29'
  },
  {
    id: '12',
    name: 'chary',
    age: '30'
  },
  {
    id: '15',
    name: 'kushal',
    age: '31'
  },
];

console.log(updateId(firstArray, secondArray));

Explanation

  • iterate over arr2 (ie, 'secondArray')
  • for name, check if arr1 (ie, 'firstArray) has a match
  • if yes, update the id by using .find
  • else, keep the info in arr2 iteration as-is
about 4 years ago · Juan Pablo Isaza Denunciar

0

How about this:

Loop through the 2nd array, look for name matches in the 1st array, If a match exists, update the 2nd array.

for (i=0;i<secondArray.length;i++){
  for(j=0; j<firstArray.length; j++){
    if(secondArray[i].name == firstArray[j].name){
      secondArray[i].id = firstArray[j].id
    } 
  }
  outputArray[i] = secondArray[i]
}

The output array would be the same as your 2nd array. If you don't want to change anything in your 2nd array, you could copy the array first and then make the changes in the double for-loop on the output array instead of the second array.

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