Check out Javascript's Array API for details on the exact syntax for Array methods.
(查看Javascript的Array API ,了解有关Array方法的确切语法的详细信息。)
Modifying your code to use the correct syntax would be:(修改代码以使用正确的语法将是:)
var array = [];
calendars.forEach(function(item) {
array.push(item.id);
});
console.log(array);
You can also use the map()
method to generate an Array filled with the results of calling the specified function on each element.
(您还可以使用map()
方法生成一个Array,其中填充了在每个元素上调用指定函数的结果。)
Something like:(就像是:)
var array = calendars.map(function(item) {
return item.id;
});
console.log(array);
And, since ECMAScript 2015 has been released, you may start seeing examples using let
or const
instead of var
and the =>
syntax for creating functions.
(而且,自ECMAScript 2015发布以来,您可能会开始使用let
或const
而不是var
和=>
语法来创建函数。)
The following is equivalent to the previous example (except it may not be supported in older node versions):(以下内容与上一个示例等效(旧版节点版本可能不支持):)
let array = calendars.map(item => item.id);
console.log(array);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…