I encountered a situation where I had 2 separate arrays:
items = [
{
offenceType:"7",
offenceCode:"JLN14",
},
{
offenceType:"48",
offenceCode:"JLN14",
}
];
demo = [
{
offenceCode: 'JLN14',
offenceType: '7',
offenceDesc: 'emergency lane abuse'
},
{
offenceCode: 'JLN14',
offenceType: '48',
offenceDesc: 'speeding'
},
{
offenceCode: 'JLN13',
offenceType: '52',
offenceDesc: 'parking abuse'
}
];
I needed to filter certain attributes between these 2 arrays. For example, in the items
array, there are offenceType
and offenceCode
values which need to be compared with the demo
array that contains offenceType
, offenceCode
, and offenceDesc
. If both offenceType
and offenceCode
match, then I need to retrieve the offenceDesc
from the demo
array. Here is the approach I tried based on my research:
newArray = [];
// create new array to map 2 objects
this.items.forEach(x => {
this.newArray.push(x.offenceCode, x.offenceType);
console.log('newArray',this.newArray);
// doing filter
})
Here is a link to my stackblitz demo
Previously, I asked a similar question on Stack Overflow but I couldn't find a solution even after multiple attempts. Being new to coding, I would appreciate any suggestions on how to solve this problem.