Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组中的Javascript循环方法_Javascript_Arrays_For Loop_Foreach - Fatal编程技术网

数组中的Javascript循环方法

数组中的Javascript循环方法,javascript,arrays,for-loop,foreach,Javascript,Arrays,For Loop,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' }]; 然后使用for()遍历数组 您可以

当我有像这样的数组中的对象时

    var bookIndex =  [{
      id: '1',
      title: 'First title',
      description: 'This is my first title'
    }, {
      id: '2',
      title: 'Second title',
      description: 'This is my second title'
    }];
然后使用for()遍历数组

您可以使用
.find()

当满足条件时,对
.find()
的回调应返回
true
。发生这种情况时,
.find()
返回数组的该元素。如果没有匹配的元素,它将返回
undefined

.forEach()
函数很有用,但它确实适用于您确实希望对数组的每个元素执行某些操作的情况。

您可以使用

some()
方法测试数组中的某个元素是否通过了所提供函数实现的测试

如果要返回book对象,则可以使用

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

您可以使用
过滤器
并返回具有该id的对象

var bookIndex=[{
id:'1',
标题:“第一个标题”,
描述:“这是我的第一个头衔”
}, {
id:'2',
标题:“第二个标题”,
描述:“这是我的第二个头衔”
}];
函数getBook(bookId){
return bookIndex.filter((e)=>{return parseInt(e.id)==parseInt(bookId)})[0];
};

console.log(getBook(2))
我想知道在哪里可以进行for循环,然后返回,因为每个女巫都回答了你没有使用的问题,所以我决定向你展示一个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);
}

bookIndex.forEach(函数(el)
中,需要向函数(回调)传递一个参数,用于forEach方法的witch。这是您的主要错误。我传递的名为el的元素基本上是数组中的每个元素,它不是未定义的或空的。而且,由于您不能仅从forEach返回某些内容,因此它返回回调,而不是parrent函数,在您的例子中是
函数getBook(index)
,我不得不调用另一个可以存储变量的函数

javascript中没有“==”操作符。将其更改为“==”。您需要数组
循环
或数组
搜索
?应该
getBook()
返回一本书或一个索引?如果只有索引,您已经有了索引,那么您只需要true或false。getBook()应该将book作为对象返回。我的意思是从bookIndex数组中返回一个对象。真正的问题是您要查找第一次出现还是所有出现….?如果要查找第一次出现,最好使用
array.prototype.find()
但是如果您需要所有的匹配项,那么
Array.prototype.filter()
是您的朋友。如果实现了find。作为Array.find()的替代方法,您可以使用Array.filter()[0]。@user2415266是的,这也可以,但是
.find()
.filter()找到匹配项时会立即停止
将始终处理整个数组。@Pointy这是一种替代方法,因为find()并不是在所有浏览器中都实现的:看起来他想取回元素,而不仅仅是它在数组中。some()仅返回true或false。同样,javascript中没有“==”运算符。如果确实要防止类型转换,请使用“==”。@NinaScholz确定这是可能的。我仍然更喜欢find()方法,但正如您正确指出的,这在所有浏览器中都不受支持。使用filter()[0]对我来说似乎是最吸引人的解决方案。@user2415266,filter正在迭代数组的所有元素。这里没有必要这样做,因为需要的元素可能位于数组的开头。对于一个短路,数组只有两种方法:some和every。有什么理由用(e)=>{}定义匿名函数吗不是函数(e){}?我最近在网上看到了很多第一种语法,而我总是使用第二种。如果返回结果,尽管返回数组。我建议返回结果[0]。这样,您将始终返回第一个找到的元素,如果未找到元素,则返回未定义的元素。由于筛选器是按Id应用的,因此无论如何,结果不应超过1个。这是一个好主意。
function getBook(bookId) {
  return bookIndex.find(function(book) { return book.id === bookId; });
}
function getBook(bookId) {  // returns true or false if the book exists
    return bookIndex.some(function (book) {
        return bookId === book.id;
    });
};
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;
};
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);
}