Can someone help me with filtering certain attributes using another array
? If a condition is met, I would like to return other attributes. Here's an example:
Array1 = [{offenceCode: 'JLN14', offenceDesc:'Speeding'}]
Array2 = [{id:0, offenceCode: 'JLN14'}, {id:1, offenceCode: 'JLN13'}]
First, I have Array2
, and I want to compare the attribute offenceCode
with Array1
. If they match, I'd like Array1
to return OffenceDesc
.
Here's what I've attempted so far:
demo = [
{
offenceCode: 'JLN14',
offenceType: '7',
offenceDesc: 'emergency lane abuse'
},
{
offenceCode: 'BRG04',
offenceType: '8',
offenceDesc: 'speeding'
},
{
offenceCode: 'CRG04',
offenceType: '9',
offenceDesc: 'emergency lane abuse'
}
];
offenceCode: any;
ngOnInit() {
this.offenceCode = this.items.map(x => x.offenceCode1);
console.log(this.offenceCode);
if(this.demo.forEach(x => x.offenceCode === this.offenceCode)) {
// return offenceDesc
}
}
I'm currently stuck and unsure how to proceed with solving this issue. You can check out my stackblitz demo. Any suggestions on how to solve this or better practices are much appreciated. Thank you!