0

I have array of objects that look like:

{
  "brandid": id,
  "brand": string,
  "id": id,
  "categoryId": id,
  "category": string,
  "factory": string,
  "series": string,
  "status": 0,
  "subStatus": 1
}

if the series property value matches another series property value in the other objects in the array, that object needs to be removed from the array.

Currently I have attempted to push them to a duplicate Array with :

      const seriesResCopy = seriesRes;
      const dupArray = []
      for (const thing of seriesResCopy) {
        for (const item of seriesRes) {
          if (thing.series === item.series) {
            dupArray.push(item);
          }
        }
      }

but this does not work. From examples I have seem my issue has been that I do not have a definite list of duplicate values to look for.

Any help would be much appreciated.

6
  • push() doesn't remove an element from an array, it just puts a reference (or copy if primitive) into the other array. Commented Jun 8, 2018 at 18:08
  • You dont actually remove anything. That can be done with .splice Commented Jun 8, 2018 at 18:09
  • 1
    So you want to filter out duplicates, triplicates, etc? Only keeping the first instance? Commented Jun 8, 2018 at 18:10
  • I am wanting to filter duplicates and keep the first instance. Commented Jun 8, 2018 at 18:11
  • @mplungjan "an array of objects like" is not "i have an object like". Don't change the question! Commented Jun 9, 2018 at 9:14

2 Answers 2

4

You could use a Set of series to filter out duplicates:

const exists = new Set();
seriesRes = seriesRes.filter(({series}) => !exists.has(series) && exists.add(series));

This uses: Array.prototype.filter, Object destructuring and some logical tricks.

The same can be done by mutating the array:

const exists = new Set();
for(const [index, {series}] of seriesRes.entries()) {
  if(!exists.has(series) {
    exists.add(series);
  } else {
    seriesRes.splice(index, 1);
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Please do explode the code and comment what it does. It is very elegant but very new (as of June 2018) too
fantastic idea with set, i have used it lately, or later or so.
@mplungjan if you change the question, why should the answers still fit?!?
@mplungjan the OP has accepted this answer. So if you say "this does not work for my weird snippet i made out of the OPs code" then this is a fault on your side, not my's.
It would not be the first time an asker posted an object and called it an array. So all I had to do was to wrap in []. That would have been useful if OP had shown a valid array in my opinion. So not so weird. Anyway your code is still interesting
|
0

To filter duplicates from the array and keep the first instance:

let seriesWithoutDuplicates = seriesRes.filter((s, i, self) => {
    return self.findIndex(z => z.series === s.series) === i;
});

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.