亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 async.map 返回數組的多個副本?

為什么 async.map 返回數組的多個副本?

Smart貓小萌 2023-03-10 13:27:10
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' } ] ,得到結果:-[ [ { name: 'john', id: '1' }, { name: 'Andrie', id: '2' } ], [ { name: 'john', id: '1' }, { name: '安德烈', id: '2' } ] ]所以我的問題是為什么它要創建數組的多個副本,如何處理呢?
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

您不必要地返回一個子數組,并且每次迭代都引用相同的數組,而您只想返回新對象。

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

不確定為什么需要異步


查看完整回答
反對 回復 2023-03-10
  • 1 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號