forEach() return 如何通过将文档作为文档数组的参数来取值?
How does forEach() return values by taking document as a parameter for an Array of Documents?
这是我对文档数组的声明:
let Arr =
[
{abc: 123 , def: 456},
{abc: 999 , def: 888},
{abc: 777 , def: 333}
];
要获取 abc 的所有值,我使用:
Arr.forEach( function (element , index)
{
console.log(element.abc);
} );
这是 forEach() 函数的标准定义。
但是我遵循的教程以不同的方式声明了数组并使用它来获取 abc 的值:
let Doc = { abc: 123 , def: 456};
let Arr = [Doc];
Arr.push({abc: 999, def: 888});
Arr.push({abc: 777, def: 333});
然后使用 forEach() 函数:
Arr.forEach(Doc => console.log(Doc.abc));
这是如何运作的?即使用 Document?
遍历
我必须以类似的方式申报吗?即首先是文档声明,然后创建包含该文档的数组,然后将其余值推入模型 ?
在第一种方法中,对象数组被声明一次。而在第二个中,创建了单独的对象,然后将其推入数组。
let Doc = { abc: 123 , def: 456}; // Separate object is created and stored in variable `Doc`.
let Arr = [Doc]; // The Object created is inserted in array, similar to `push`.
Arr.push({abc: 999, def: 888}); // Object is directly pushed into the array, without creation of separate variable and storing object in it.
Arr.push({abc: 777, def: 333});
在第一个 forEach
中,常规函数用作回调,而在第二个 arrow based
中,函数用作回调。
Arr.forEach(Doc => console.log(Doc.abc));
-> 在这个Doc
中引用对象。 Doc
不引用变量 let Doc = { abc: 123 , def: 456};
。除了 Doc
,您还可以提供不同的名称。
let Doc = { abc: 123 , def: 456};
let Arr = [Doc];
Arr.push({abc: 999, def: 888});
Arr.push({abc: 777, def: 333});
Arr.forEach(Doc => {
console.log(Doc);
});
这是我对文档数组的声明:
let Arr =
[
{abc: 123 , def: 456},
{abc: 999 , def: 888},
{abc: 777 , def: 333}
];
要获取 abc 的所有值,我使用:
Arr.forEach( function (element , index)
{
console.log(element.abc);
} );
这是 forEach() 函数的标准定义。
但是我遵循的教程以不同的方式声明了数组并使用它来获取 abc 的值:
let Doc = { abc: 123 , def: 456};
let Arr = [Doc];
Arr.push({abc: 999, def: 888});
Arr.push({abc: 777, def: 333});
然后使用 forEach() 函数:
Arr.forEach(Doc => console.log(Doc.abc));
这是如何运作的?即使用 Document?
遍历
我必须以类似的方式申报吗?即首先是文档声明,然后创建包含该文档的数组,然后将其余值推入模型 ?
在第一种方法中,对象数组被声明一次。而在第二个中,创建了单独的对象,然后将其推入数组。
let Doc = { abc: 123 , def: 456}; // Separate object is created and stored in variable `Doc`.
let Arr = [Doc]; // The Object created is inserted in array, similar to `push`.
Arr.push({abc: 999, def: 888}); // Object is directly pushed into the array, without creation of separate variable and storing object in it.
Arr.push({abc: 777, def: 333});
在第一个 forEach
中,常规函数用作回调,而在第二个 arrow based
中,函数用作回调。
Arr.forEach(Doc => console.log(Doc.abc));
-> 在这个Doc
中引用对象。 Doc
不引用变量 let Doc = { abc: 123 , def: 456};
。除了 Doc
,您还可以提供不同的名称。
let Doc = { abc: 123 , def: 456};
let Arr = [Doc];
Arr.push({abc: 999, def: 888});
Arr.push({abc: 777, def: 333});
Arr.forEach(Doc => {
console.log(Doc);
});