1

Say I have an array of 5 objects, each with 2 keys (eg, 'title' & 'author').

I want to check the truthiness that 3 SPECIFIC titles exist in the array.

What's the best way to do that?

I have the following... but it doesn't seem very efficient:

const books = [
 { title: 'Book1', author: 'Author1' },
 { title: 'Book2', author: 'Author2' },
 { title: 'Book3', author: 'Author3' },
 { title: 'Book4', author: 'Author4' },
 { title: 'Book5', author: 'Author5' },
];
const certainBooks = books.some((b) => b.title === 'Book2')
 && books.some((b) => b.title === 'Book3')
 && books.some((b) => b.title === 'Book5')
if (certainBooks) {
 // Do stuff
}
asked Mar 22, 2022 at 23:22
2
  • 2
    Is this something you need to do often? Are the three specific titles dynamic or fixed? Is it always three? Commented Mar 22, 2022 at 23:26
  • This function could get 5k hits/mo, so it'd be fairly high traffic. The 3 titles ARE specific though - I'd always be looking for the same 3 in a possible array of 1-5 books. Commented Mar 23, 2022 at 0:07

4 Answers 4

2

If the values and number of titles is dynamic, it might be worth creating an index of titles in the array; something with O(1) time complexity for faster lookups

const books = [
 { title: 'Book1', author: 'Author1' },
 { title: 'Book2', author: 'Author2' },
 { title: 'Book3', author: 'Author3' },
 { title: 'Book4', author: 'Author4' },
 { title: 'Book5', author: 'Author5' },
];
const titleIndex = new Set(books.map(({ title }) => title));
const titlesExist = (...titles) =>
 titles.every(title => titleIndex.has(title))
console.log("Book2, Book3, Book5:", titlesExist("Book2", "Book3", "Book5"));
console.log("Book1:", titlesExist("Book1"));
console.log("Book5, Book6:", titlesExist("Book5", "Book6"));

answered Mar 22, 2022 at 23:31
Sign up to request clarification or add additional context in comments.

1 Comment

Nice call out! In this case, it'd only be dynamic on a request-by-request basis. So a client could send in 1-5 books, and I'm checking to make sure 3 specific ones exist on that request before I do some work.
1

A more general approach would be to map the books to their titles, then check that .every one of the titles you're looking for exists.

const books = [
 { title: 'Book1', author: 'Author1' },
 { title: 'Book2', author: 'Author2' },
 { title: 'Book3', author: 'Author3' },
 { title: 'Book4', author: 'Author4' },
 { title: 'Book5', author: 'Author5' },
];
const titles = books.map(({ title }) => title);
const toFind = ['Book2', 'Book3', 'Book5'];
if (toFind.every(title => titles.includes(title))) {
 console.log('do stuff');
}

If the array of books is large, you could benefit by making titles a Set instead of an array - Set#has is faster than Array#includes when there are a lot of elements.

answered Mar 22, 2022 at 23:27

Comments

0

You could loop over them

const books = [
 { title: "Book1", author: "Author1" },
 { title: "Book2", author: "Author2" },
 { title: "Book3", author: "Author3" },
 { title: "Book4", author: "Author4" },
 { title: "Book5", author: "Author5" },
];
const booksNeeded = ["Book2", "Book3", "Book4"];
for (let book of books) {
 const lookForIndex = booksNeeded.findIndex(
 (title) => title.toLowerCase() === book.title.toLowerCase()
 );
 if (lookForIndex !== -1) {
 booksNeeded.splice(lookForIndex, 1);
 }
 if (!booksNeeded.length) {
 break; // Early break if all the books has been found
 }
}
if (!booksNeeded.length) {
 console.log("Do Something");
} else {
 console.log("Something else");
}
answered Mar 22, 2022 at 23:50

Comments

0

const books = [
 { title: 'Book1', author: 'Author1' },
 { title: 'Book2', author: 'Author2' },
 { title: 'Book3', author: 'Author3' },
 { title: 'Book4', author: 'Author4' },
 { title: 'Book5', author: 'Author5' },
];
let ops = 0;
let search = [ "Book2", "Book3", "Book4" ];
let { length } = search;
for ( let i = 0, len = books.length; length && i < len; i++ ){
 ops++;
 if ( search.includes(books[i].title) ){
 length--;
 }
}
if ( !length ){
 console.log("All books found!");
} else {
 console.log("Not all books found!")
}
console.log( "Number of operations: ", ops ); 

answered Mar 23, 2022 at 0:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.