blankface
blankface

Reputation: 6347

Remove duplicate substring in an array

I have an array like below:

[
  'author/2020/01/01/all_authors_000.csv',
  'book/2020/01/01/all_books_000.csv',
  'book/2020/01/01/all_books_001.csv',
  'book/2020/01/01/all_books_002.csv',
  'others/2020/01/01/other_stuff.csv',
]

As you can see there are three items that start with the word book. I want to remove all but one, so I end up with something like:

[
  'author/2020/01/01/all_authors_000.csv',
  'book/2020/01/01/all_books_000.csv',
  'others/2020/01/01/other_stuff.csv',
]

How can I achieve this?

Upvotes: 2

Views: 376

Answers (2)

Behemoth
Behemoth

Reputation: 9300

Here is another method of doing it. Basically a function that takes in your array and a criteria to identify duplicates (in your case book). All of these duplicates will be removed but the first one.

const array = [
  "author/2020/01/01/all_authors_000.csv",
  "book/2020/01/01/all_books_000.csv",
  "book/2020/01/01/all_books_001.csv",
  "book/2020/01/01/all_books_002.csv",
  "others/2020/01/01/other_stuff.csv",
];

const removeDuplicates = (array, criteria) => {
  return array.filter(
    (path) =>
      ![...array.filter((path) => path.includes(criteria)).splice(1)].includes(
        path
      )
  );
};

console.log(removeDuplicates(array, "book"));

Upvotes: 0

David Malášek
David Malášek

Reputation: 1428

Here is working example:

var array = [
   "author/2020/01/01/all_authors_000.csv",
   "book/2020/01/01/all_books_000.csv",
   "book/2020/01/01/all_books_001.csv",
   "book/2020/01/01/all_books_002.csv",
   "others/2020/01/01/other_stuff.csv",
];

var filteredArray = [];
var previous = "";

for (let i of array) {
   if (i.substr(0, i.indexOf("/")) != previous) {
      filteredArray.push(i);
      previous = i.substr(0, i.indexOf("/"));
   }
}

Every loop the value before "/2020" is stored inside the previous variable, and the if statement checks, if the value is the same as in the previous loop. If not, it pushes it into the filteredArray.

Therefore filteredArray is the array without duplicates.

Upvotes: 3

Related Questions