0

I have an object array with multiple variables.

let Books = [];

class Book {
  constructor(ISBN, title, author, edition, publication, year, section) {
    this.ISBN = ISBN;
    this.title = title;
    this.author = author;
    this.publication = publication;
    this.year = year;
    this.edition = edition;
    this.section = section;
  }
}

//Example
ISBN = "978-1-56619-909-4";
title = "Software Engineering";
authors = ["author1", "author2"];
edition = 1;
publication = "Book publisher";
year = 2021;
section = "Engineering";

temp = new Book(ISBN, title, authors, edition, publication, year, section);
Books.push(temp);

I have a search bar on my website that I use to search and filter certain variables in the array based on user input. For example, if a user enters a word, I look for matches in the title variable and also in three other variables (author, publication and section) to get more results.

function search() {
  filtered_results = [];
  let searchText = searchBar.value;
  if (searchText != "") {
    let searchQuery = new RegExp(searchText, 'i');
    temp_filter = Books.filter(book => searchQuery.test(book.title));
    temp_filter = temp_filter.concat(Books.filter(book => searchQuery.test(book.author)));
    temp_filter = temp_filter.concat(Books.filter(book => searchQuery.test(book.publication)));
    temp_filter = temp_filter.concat(Books.filter(book => searchQuery.test(book.section)));
  } else {
    temp_filter = Books;
  }

  //Removing duplicates
  filtered_results = removeDupicates(temp_filter);
  // Display filtered results
  displayResults(filtered_results);
}

The problem is sometimes I end up having duplicates because of matches in multiple variables. In the above example, I have the word 'Engineering' in both title and section, so it is displayed twice. I am trying to eliminate the duplicates using ISBN (since 2 books cannot have the same ISBN).

//Function to remove duplicates
function removeDupicates(array) {
  let uniqueArray = [];
  for (i = 0; i < array.length; i++) {
    testID_t = array[i].ISBN;
    let testID = new RegExp(testID_t);
    if (!(testID.test(uniqueArray.ISBN))) {
      uniqueArray.push(array[i]);
    }
  }

  console.log(uniqueArray);
  return uniqueArray;
}

But I still end up with duplicates because uniqueArray.ISBN is undefined and the test expression always evaluates to true. How do I check the ISBN values of all the elements of uniqueArray?

7
  • 1
    Just put all your logic in 1 filter... Commented Apr 16, 2021 at 6:52
  • Why would you expect an Array to have a property called ISBN? That's a property of Book objects, not Arrays. Commented Apr 16, 2021 at 6:53
  • @mario_sunny but all my Book objects are inside the array Commented Apr 16, 2021 at 7:01
  • @Keith I want to keep it separate because I want to add an advanced search later where users can search more specifically Commented Apr 16, 2021 at 7:04
  • 1
    An array is just an object that contains other objects; it's not aware of the type of the objects that it's holding. You need to apply the test to each element in the array, not the array itself. In this particular case you can use the builtin Array.prototype.some function: !uniqueArray.some(book => testID.test(book.ISBN)) Commented Apr 16, 2021 at 7:08

2 Answers 2

2

You can construct a Set out of the array. This would work because your array would be having references to the same object.

In the code snippet below, arr has two copies of the javascript object, but unique only has a single copy.

class Book {
  constructor(ISBN, title) {
    this.ISBN = ISBN,
    this.title = title
  }
}

const 
  javascript = new Book(12, "Javascript"),
  golang = new Book(23, "Golang"),
  arr = [javascript, golang, javascript],
  removeDupicates = (arr) => [...new Set(arr)],
  unique = removeDupicates(arr);

console.log(unique);

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

3 Comments

@Gekkouga Please check if this solves your problem, let me know if you have any issues.
@SomShekarMukherjee Yes. It indeed solves the problem. But I'm curious about how it identifies the duplicates without knowing which variables of the object to compare. Does it check every single variable in the object? If that's the case wouldn't it take more time and resource than comparing a specific variable?
@Gekkouga It doesn't check the properties present inside the object, it checks the underlying reference, because your array has references to the same object again and again using Set is quite performant. If you had some array where objects would have been same only by their properties, then you would have to explicitly specify by which property you wish to make the array unique. And to make something unique, you should always try to consider using Sets because that is what Sets are meant for.
1

I suggest you to use Lodash: https://lodash.com/docs/4.17.15#sortedUniq.

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.