简体   繁体   English

Array方法中的Javascript循环

[英]Javascript loop in Array method

When I have objects in Array like 当我在Array中有对象时

    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() 然后使用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. 的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;
      });
    };

You'd use .find() : 你使用.find()

function getBook(bookId) {
  return bookIndex.find(function(book) { return book.id === bookId; });
}

The callback to .find() should return true when the criteria are satisfied. 当满足条件时, .find()的回调应该返回true When that happens, .find() returns that element of the array. 当发生这种情况时, .find()返回该数组的元素。 If no elements match, it returns undefined . 如果没有元素匹配,则返回undefined

The .forEach() function is useful, but it really is for situations when you actually do want to perform some operation on each element of the array. .forEach()函数很有用,但实际上它确实适用于您希望对数组的每个元素执行某些操作的情况。

You could use Array#some() 你可以使用Array#some()

The some() method tests whether some element in the array passes the test implemented by the provided function. some()方法测试数组中的某个元素是否通过了由提供的函数实现的测试。

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;
};

You can use filter and return object with that id. 您可以使用带有该id的filter和return对象。

 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)) 

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 我想你在哪里想知道你怎么能循环然后回来,因为每个女巫都回答你并没有使用每一个我想要向你展示一个foreach解决方案,希望这是你所期望的

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 the bookIndex.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 case function getBook(index) , i had to call another functuion in witch i can store the variable bookIndex.forEach( function (el)你需要将一个参数传递给函数(回调),你用于forEach方法。这是你的主要错误。我通过的这个elment基本上是每个你的数组中的元素不是未定义的或者是null。因为你不能只从foreach返回一些东西,导致它返回到回调函数,而不是parrent函数,在你的case function getBook(index) ,我有在巫婆中调用另一个功能,我可以存储变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM