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

161
Vistas
Cannot combine object array

Hello I am trying to combine two arrays but why the result only show the staff name and the rest is undefined?

let cuba2 = [];
let cuba3 = [];  
 
let staff = [
  {name:"tim", postcode:"68100",counter:0},
  {name:"sam", postcode:"43500",counter:0},
  {name:"apu", postcode:"55100",counter:0}
];

let cuba2 = [
  {fsm:"111", postcode:"68100"},
  {fsm:"222", postcode:"55555"},
  {fsm:"333", postcode:"66666"}
];

cuba2.forEach(data => {
  staff.forEach(assign => {
    if(data.postcode == assign.postcode{
      cuba3.push({
          fsm:data.FSM,
          postcode: data.POSCODE,
          staff:assign.name
      });
    }
  });
});
console.log(cuba3);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It's not perfectly clear from your code if you really wanted the result array to be the perfect join of the two input arrays using the key postcode for matching.

By the way that's what this demo attempts to achieve:

it loops through the two arrays staff and cuba2, producing a third array cuba3 having items that have the same postcode and holding the fsm property from the cuba item and the staff property from the staff item:

let staff = [
  {name:"tim", postcode:"68100",counter:0},
  {name:"sam", postcode:"43500",counter:0},
  {name:"apu", postcode:"55100",counter:0}
];

let cuba2 = [
  {fsm:"111", postcode:"68100"},
  {fsm:"222", postcode:"55555"},
  {fsm:"333", postcode:"66666"}
];

let cuba3 = [];  

//for each staffItem in staff
for(let staffItem of staff){  
  //for each cubaItem in cuba2
  for(let cubaItem of cuba2){
    //if an item in cuba2 was found matching staffItem.postCode
    if(cubaItem.postcode == staffItem.postcode)  {
      //push a new item in cuba3 with a mix of properties coming both from cubaItem and staffItem
      cuba3.push({
        fsm: cubaItem.fsm,
        postcode: cubaItem.postcode,
        staff: staffItem.name
      });
      //since the corresponding cubaItem was found, skip to the next iteration of staffItem
      continue;
    }
  }
}

console.log(cuba3);
/*
The result will be an array with lenght == 1
because the two sets will interesect only for postcode == '68100' 
[
  {
    fsm: "111"
    postcode: "68100"
    staff: "tim"
  }
]
*/

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here you can do it in two different ways.

  1. You can loop through first array and inside that loop you can loop through the other array

let cuba3 = [];  
 
let staff = [
  {name:"tim", postcode:"68100",counter:0},
  {name:"sam", postcode:"43500",counter:0},
  {name:"apu", postcode:"55100",counter:0}
];

let cuba2 = [
  {fsm:"111", postcode:"68100"},
  {fsm:"222", postcode:"55555"},
  {fsm:"333", postcode:"66666"}
];

cuba2.forEach(cuba => {
    staff.forEach(assign => {
        if (cuba.postcode == assign.postcode) {
            cuba3.push({
                fsm: cuba.fsm,
                postcode: cuba.postcode,
                staff: assign.name
            });
        }
    })
});

console.log(cuba3);

However the first one isn't very efficient. It has nested loops. We can use hashmaps (or objects) in JavaScript like the following:

const cuba3 = [];  
 
let staff = [
  {name:"tim", postcode:"68100",counter:0},
  {name:"sam", postcode:"43500",counter:0},
  {name:"apu", postcode:"55100",counter:0}
];

let cuba2 = [
  {fsm:"111", postcode:"68100"},
  {fsm:"222", postcode:"55555"},
  {fsm:"333", postcode:"66666"}
];

const staffPostcodes = {};

for (let i=0; i < staff.length; i++) {
    staffPostcodes[staff[i].postcode] = staff[i].name;
}

for (let i=0; i < cuba2.length; i++) {
    if (!staffPostcodes[cuba2[i].postcode]) {
        continue;
    }

    cuba3.push({
        fsm: cuba2[i].fsm,
        postcode: cuba2[i].postcode,
        staff: staffPostcodes[cuba2[i].postcode]
    });
}

console.log(cuba3);

This one will output the same result but much faster when the array becomes very large.

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