为什么 async.map returns 数组的多个副本?

Why async.map returns multiple copies of array?

const async = require('async');

const arr = [
    { name: 'john', id: '1' },
    { name: 'Andrie', id: '2' }]

let collectArr = [];
let data = async.mapLimit(arr, 5, async function (input) {

    collectArr.push({ name: input.name, id: input.id });
    return collectArr;
})

data.then((result) =>{
    console.log('success',result);
}).catch(e => console.log('err'));

所以我在这里向 async.mapLimit 提供数组,没有回调并期待这里的承诺。 预期输出:- [ { name: 'john', id: '1' }, { name: 'Andrie', id: '2' } ] ,

得到结果:-

[ [ { 名称: 'john', id: '1' }, { 名称: 'Andrie', id: '2' } ], [ { 名称:'john',id:'1'},{ 名称:'Andrie',id:'2'}]]

所以我的问题是为什么它会创建多个数组副本,如何处理?

你不必要地 return 创建一个子数组,并且每次迭代引用同一个数组,而你想要的只是 return 新对象。

let data = async.mapLimit(arr, 5, async function (input) {
    return { name: input.name, id: input.id };   
});

不确定为什么需要异步