0

This question might be a simple iteration, but I'm stuck in the logic.

I have an array of data which needs to be iterated based on id and code and remove the data only when the code is not present in the given id's.

Here is the Case Scenarios, From the below data,

  1. There are two different code named "GOOGLE" with different id - Valid Case
  2. There are two different code named "FACEBOOK" with different id - Valid Case
  3. There is a code named "TWITTER" which is not present in another id - INVALID Case.

Here, I wanted to remove this data based on case 3.

 {
  "id" : 378,
  "code" : "TWITTER",
  "comment" : "zeeer"
}

Can someone help me with this scenario?

****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 tried something below, but not sure how to proceed after this.

And, I'm using angular 7 and it will be helpful if I get the solution based on typescript.

this.data.forEach((row, index) => {
        let value = row.id;
        if(originalArray.indexOf(value) == -1) {
          console.log(value);
        }
        originalArray.push(row);
     })
4
  • This question (or at least, the code you've provided) is not related to Angular, nor Typescript. It's just plain old JavaScript. Commented Jul 19, 2019 at 13:47
  • Yes, I just given a sample with javascript, not sure how to proceed from here Commented Jul 19, 2019 at 13:52
  • Also, your logic just seems a bit unclear, to me at least. Is the logic essentially "Remove any ID+Code pair that appears only once"? Commented Jul 19, 2019 at 13:52
  • "The logic is to remove any ID+Code pair not matched ", then case 1 and case 2 scenarios also not valid right - (GOOGLE 378 is not matching with GOOGLE 381) Commented Jul 19, 2019 at 14:07

3 Answers 3

1

try this:

 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"
} ]

var result = data.reduce((unique, o) => {
    if(data.filter(obj => obj.id != o.id && obj.code === o.code).length>=1) {
      unique.push(o);
    }
    return unique;
},[]);

console.log(result);

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Ghoul :) Using ES6 reduce is a good approach. Kudos :)
But, if we don't have duplicate, it is giving empty array
You said "The logic is to remove any ID+Code pair not matched". What does "matched" mean if not "duplicated"?
@UI_Dev, can you explain what do you mean not duplicated !?
what data did you use that it is giving empty array ?
|
1

Your logic that "Any code + Id pair that is not matched is removed" seems to directly contradict your first two cases, given that those pairs aren't matched - the ID is different.

Regardless, the code below will only keep items that have duplicate code with different id.

const 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" } ];
let result = data.filter(({id,code}) => !!data.find(obj => obj.code === code && obj.id !== id));
console.log(result);

Optimally you wouldn't need nested array methods here, and rather a lookup/hashmap to prevent unnecessary iteration, but seeing that you already accepted an answer I'll forego that for now.

3 Comments

I really appreciate your effort. But, If I don't have duplicates, it is throwing empty array. For example, if we have only first two set of data from the given array, it is throwing empty response.
In the above case, Both are same id, so it should return 2 data
"For example, if we have only first two set of data from the given array" - How is that any different from Twitter in the first example? It has the same ID as others but you said to exclude it because "it's not present in another id". "There are two different code named "GOOGLE" with different id" - If you only have the first two sets of data from the given array, as you suggest, then this is not true. Why would it be included?
0

With Lodash

const 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"
    } ];

    const groupedData = _.groupBy(data, 'code'); // import * as _ from 'lodash'

    console.log(data.filter(entry => groupedData[entry.code].length > 1));

Result:

[Object, Object, Object, Object]
0: Object
code: "GOOGLE"
comment: "ffff"
id: 381
__proto__: Object
1: Object
code: "FACEBOOK"
comment: "fff"
id: 381
__proto__: Object
2: Object
code: "GOOGLE"
comment: "rferer"
id: 378
__proto__: Object
3: Object
code: "FACEBOOK"
comment: "fefehh"
id: 378
__proto__: Object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.