When I have objects in Array like
var bookIndex = [{ id: '1', title: 'First title', description: 'This is my first title' }, { id: '2', title: 'Second title', description: 'This is my second title' }];then loop through array using for()
function getBook(bookId){ for (var i = 0; i < bookIndex.length; i++) { if (bookIndex[i].id === bookId) { return bookIndex[i]; } } return undefined; };I wonder how to use other loop method, to get same result. Ex. forEach.I try to use something like this but it couldn't get return object I want.
function getBook(bookId) { bookIndex.forEach(function () { if (bookId === bookIndex.id) { return bookId; } return undefined; }); };- 1There is no '====' operator in javascript. Change it to '==='.randominstanceOfLivingThing– randominstanceOfLivingThing2016-05-11 14:34:59 +00:00CommentedMay 11, 2016 at 14:34
- You need to array
loopor arraysearch?Mohammad– Mohammad2016-05-11 14:36:30 +00:00CommentedMay 11, 2016 at 14:36 - should
getBook()return a book or an index? if just the inde, the you have already one, then you need only true or false.Nina Scholz– Nina Scholz2016-05-11 14:40:33 +00:00CommentedMay 11, 2016 at 14:40 - getBook() should return book as an object. I mean 1 object from bookIndex array.Mac L. Lak– Mac L. Lak2016-05-11 14:56:01 +00:00CommentedMay 11, 2016 at 14:56
- 1The real question is do you want to find the first occurrence or all occurrences..? If you want the first occurrence you best use
Array.prototype.find()but if you need all occurrences thenArray.prototype.filter()is your friend.Redu– Redu2016-05-11 15:51:41 +00:00CommentedMay 11, 2016 at 15:51
4 Answers4
You'd use.find():
function getBook(bookId) { return bookIndex.find(function(book) { return book.id === bookId; });}The callback to.find() should returntrue when the criteria are satisfied. When that happens,.find() returns that element of the array. If no elements match, it returnsundefined.
The.forEach() function is useful, but it really is for situations when you actually do want to perform some operation oneach element of the array.
4 Comments
.find() stops as soon as it finds a match while.filter() will always process the entire array.You can usefilter and return object with that id.
var bookIndex = [{ id: '1', title: 'First title', description: 'This is my first title' }, { id: '2', title: 'Second title', description: 'This is my second title' }]; function getBook(bookId) { return bookIndex.filter((e) => { return parseInt(e.id) == parseInt(bookId)})[0]; }; console.log(getBook(2))3 Comments
You could useArray#some()
The
some()method tests whether some element in the array passes the test implemented by the provided function.
function getBook(bookId) { // returns true or false if the book exists return bookIndex.some(function (book) { return bookId === book.id; });};For returning the book object, then you might use
function getBook(bookId) { // returns the book with the index var book; bookIndex.some(function (b) { if (bookId === b.id) { book = b; return true; } }); return book;};4 Comments
i tought you where wondering how could you for loop and then return, and since every one witch answered you didn't used for each i desided to show you a foreach solution, hoping that this is what you had expected
var bookIndex = [ { id: '1', title: 'First title', description: 'This is my first title' }, { id: '2', title: 'Second title', description: 'This is my second title' }];function getBook(bookId) { bookIndex.forEach( function (el) { if (el.id === bookId) { getBook1(el); } });}getBook('2');function getBook1(el) { var element = el; console.log(element);}In thebookIndex.forEach( function (el)you need to pass an argument to the function (the callback), witch you use for the forEach method. And this was your main mistake. This elment that i have passed called el is basically every element in your array witch isn't undefined or null. And since you can't just return something from the foreach, cause it returns in to the callback, not to the parrent function, in your casefunction getBook(index), i had to call another functuion in witch i can store the variable
Comments
Explore related questions
See similar questions with these tags.

