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

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

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 Relatório

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 Relatório

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 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