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);
CodePudding user response:
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"
}
]
*/
CodePudding user response:
Here you can do it in two different ways.
- 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.