0

I was wondering how to use -this- with a function parameter, in a JS object

How can i specify that -books- is going to be a value from the object when calling the function ?

res.write(tableGenerator.bookTable(data.sortBooks(data.englishBooks)));

const data = {
  englishBooks: [
    {
      title: "The Fellowship of the Ring",
      language: "English",
      country: "United Kingdom",
      author: "J.R.R. Tolkien",
      date: "1954-07-29",
    },
    {
      title: "Prelude to foundation",
      language: "English",
      country: "United States",
      author: "Isaac Asimov",
      date: "1988-11-08",
    }
  ],
  sortBooks(books) {
    return books.sort(
      (a, b) => a.date.substring(0, 4) - b.date.substring(0, 4)
    );
  },
};

Thanks !

I tried the code below :

res.write(tableGenerator.bookTable(data.sortBooks(englishBooks)));

sortBooks(books) {
    return this.books.sort(
      (a, b) => a.date.substring(0, 4) - b.date.substring(0, 4)
    );
  },
Enzo
  • 51
  • 2
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this – rajniszp Nov 02 '22 at 08:52
  • 1
    If its an object literal, it will be `data.books`. This comes in play when there is a constructor involved – Rajesh Nov 02 '22 at 08:53
  • 1
    Why are you trying to sort `this.books` when your object doesn't even have a `books`property and ignoring the array that is passed to the function using the `books` parameter? – phuzi Nov 02 '22 at 08:55

0 Answers0