I seem to be struggling with the logic behind this seemingly straightforward iteration question.
My task involves iterating through an array of data based on id and code, removing data only when the code is not associated with the given id's.
Let's consider some case scenarios:
- Two different ids have entries for "GOOGLE" - Valid Case
- Two different ids have entries for "FACEBOOK" - Valid Case
- An entry for "TWITTER" doesn't match any other id - INVALID Case.
In this specific scenario, I need help removing the data related to case 3.
{
"id" : 378,
"code" : "TWITTER",
"comment" : "zeeer"
}
Any assistance or input on solving this problem would be greatly appreciated.
****Below is the original array data****
data = [ {
"id" : 381,
"code" : "GOOGLE",
"comment" : "ffff"
}, {
"id" : 381,
"code" : "FACEBOOK",
"comment" : "fff"
}, {
"id" : 378,
"code" : "TWITTER",
"comment" : "zeeer"
}, {
"id" : 378,
"code" : "GOOGLE",
"comment" : "rferer"
}, {
"id" : 378,
"code" : "FACEBOOK",
"comment" : "fefehh"
} ]
I've attempted a solution below, but I'm uncertain about the next steps.
Furthermore, the framework I am using is Angular 7, so solutions tailored to typescript would be most beneficial.
this.data.forEach((row, index) => {
let value = row.id;
if(originalArray.indexOf(value) == -1) {
console.log(value);
}
originalArray.push(row);
})