Collect.js eachSpread() 方法
eachSpread() 方法用于迭代集合项并将集合的每个嵌套项值传递给给定的回调函数。
句法:
collection.eachSpread()
参数: collect() 方法接受一个参数,该参数转换为集合,然后对其应用 eachSpread() 方法,如果将其应用于对象集合,则可以获取元素。
返回值:此方法迭代项目的集合。
下面的示例说明了 collect.js 中的 eachSpread() 方法:
示例 1:
Javascript
const collect = require('collect.js');
const collection = collect([
['Rakesh', 80],
['Shyam', 94],
['Sandeep', 75],
['Ashok', 88]
]);
collection.eachSpread((name, marks) => {
console.log("Name => " + name
+ " | Marks => " + marks)
});
Javascript
const collect = require('collect.js');
let arr = [
['Rahul', 98],
['Aditya', 96],
['Abhishek', 80],
];
// Converting object to collection
const collection = collect(arr);
collection.eachSpread((name, score) => {
console.log(name, score);
});
console.log(collection.eachSpread(
(name, score) => false)
);
输出:
Name => Rakesh | Marks => 80
Name => Shyam | Marks => 94
Name => Sandeep | Marks => 75
Name => Ashok | Marks => 88
示例 2:
Javascript
const collect = require('collect.js');
let arr = [
['Rahul', 98],
['Aditya', 96],
['Abhishek', 80],
];
// Converting object to collection
const collection = collect(arr);
collection.eachSpread((name, score) => {
console.log(name, score);
});
console.log(collection.eachSpread(
(name, score) => false)
);
输出:
Rahul 98
Aditya 96
Abhishek 80
Collection {
items: [
[ 'Rahul', 98 ],
[ 'Aditya', 96 ],
[ 'Abhishek', 80 ]
]
}