2

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?

blankface
  • 5,757
  • 19
  • 67
  • 114

2 Answers2

3

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.

Daweed
  • 1,419
  • 1
  • 9
  • 24
  • thank you. do you know if this can also be achieved by lodash? – blankface Mar 22 '21 at 07:37
  • @blankface I don't have experience with lodash myself, but take a look at [this](https://stackoverflow.com/questions/31740155/lodash-remove-duplicates-from-array). – Daweed Mar 22 '21 at 07:39
0

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"));
Behemoth
  • 5,389
  • 4
  • 16
  • 40