I've encountered an issue while trying to merge two arrays and create a new one. It seems that my forEach loop inside the else statement is returning undefined. I'm unsure if I made a mistake in my approach or if forEach is not meant to be used within an else statement. If there's a better way to achieve the desired result, please advise.
stackblitz : https://stackblitz.com/edit/angular-ivy-145nbf?file=src%2Fapp%2Fapp.component.ts
public merged = [];
public ArrayOne = [
{
time: "05:00 PM",
maxNumber: 4
},
{
time: "05:30 PM",
maxNumber: 4
},
{
time: "06:30 PM",
maxNumber: 4
}
];
public ArrayTwo = [
{
active: 2,
time: "05:00 PM"
}
];
mergeArray() {
let t = this.ArrayOne.map((element, i) => {
let d = {
time: element.time,
maxNumber: element.maxNumber,
active: this.getActive(this.ArrayTwo)
};
this.merged.push(d);
console.log(d);
});
}
getActive(arr2) {
if (arr2.length === 0 || arr2 === null || arr2 === undefined) {
return 0;
} else {
arr2.forEach((element, i) => {
if (element.time === this.ArrayOne[i].time) {
return element.active;
} else {
return 0;
}
});
}
}
Expected Result
public merged = [
{
time: "05:00 PM",
maxNumber: 4,
active: 2
},
{
time: "05:30 PM",
maxNumber: 4,
active: 0
},
{
time: "06:30 PM",
maxNumber: 4,
active: 0
}
];