Collect.js mapToDictionary() 方法
mapToDictionary() 方法用于在集合项上运行字典映射。给定的回调函数返回一个具有单个 (key, value) 对的关联数组。
句法:
collect(array).mapToDictionary(callback)
参数: collect() 方法采用一个参数,该参数转换为集合,然后对其应用 mapToDictionary() 方法。 mapToDictionary() 方法将回调函数作为参数保存。
返回值:此方法返回具有单个(键,值)对的关联数组。
下面的示例说明了 collect.js 中的 mapToDictionary() 方法:
示例 1:
Javascript
const collect = require('collect.js');
let obj = [{
name: 'Ashok',
score: 75
},
{
name: 'Rakesh',
score: 86
},
{
name: 'Rajesh',
score: 56
}];
const collection = collect(obj);
const sequence = collection.mapToDictionary(
element => [element.name, element.score]);
console.log(sequence.all());
Javascript
const collect = require('collect.js');
let obj = [
{
name: 'Rahul',
dob: '25-10-96',
},
{
name: 'Aditya',
dob: '25-10-96',
},
{
name: 'Abhishek',
dob: '16-08-94',
},
{
name: 'Rahul',
dob: '19-08-96',
},
];
const collection = collect(obj);
const sequence = collection.mapToDictionary(
element => [element.name, element.dob]);
console.log(sequence.all());
输出:
{ Ashok: [ 75 ], Rakesh: [ 86 ], Rajesh: [ 56 ] }
示例 2:
Javascript
const collect = require('collect.js');
let obj = [
{
name: 'Rahul',
dob: '25-10-96',
},
{
name: 'Aditya',
dob: '25-10-96',
},
{
name: 'Abhishek',
dob: '16-08-94',
},
{
name: 'Rahul',
dob: '19-08-96',
},
];
const collection = collect(obj);
const sequence = collection.mapToDictionary(
element => [element.name, element.dob]);
console.log(sequence.all());
输出:
{
Rahul: [ '25-10-96', '19-08-96' ],
Aditya: [ '25-10-96' ],
Abhishek: [ '16-08-94' ]
}