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?

4
  • Technically your question says Remove duplicate substring in an array and there is no duplicate. Commented Mar 22, 2021 at 7:25
  • The substring book/ is being duplicated Commented Mar 22, 2021 at 7:27
  • what is the criteria. are book/2020 and book/2021 also dublicates? Commented Mar 22, 2021 at 7:32
  • No, just the word book. Commented Mar 22, 2021 at 7:36

2 Answers 2

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.

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

2 Comments

thank you. do you know if this can also be achieved by lodash?
@blankface I don't have experience with lodash myself, but take a look at this.
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"));

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.