I am not sure how to accurately describe this issue, but the code should suffice. This code is in React.js, but I think it is a JavaScript question more than anything. I have a variable initialization:
const key = this.state.key;
This will evaluate to one of three strings...title, author, or year. How can I use this variable in the following block?
let filteredBooks = books.filter(
(book) => {
return book.key.toString().toLowerCase().indexOf(this.state.query) !== -1;
}
);
Book has properties title, author, and year... that I want to query, and I would rather not switch through the possible values of key(only 3 right now), in case I want to scale with more properties for the book object later. How can I dynamically reference key, and render it on the fly to either title, author, year, or any other property that key might be. (key is not a property of book). I have tried book.eval(key), but this throws an "undefined is not a function" compilation error at eval(key). Once again, I am using React.js but I am thinking this is a general Javascript question. Thanks!