如何将for循环转换为map函数

Posted

技术标签:

【中文标题】如何将for循环转换为map函数【英文标题】:How to convert for loop to map function 【发布时间】:2020-01-25 09:51:44 【问题描述】:

如何将model[i].bulkCreate(dtarr[i]);转换为map函数?

        const dtarr = [dataItems.Container_Type];
        const model = [Container]

        for (var i=0; i < model.length; i++) 
            model[i].bulkCreate(dtarr[i]);
        

【问题讨论】:

请更正代码格式,并添加问题。 对不起我的格式 Index inside map() function 的可能重复项。我假设您的问题是您不知道如何访问您正在迭代的集合的索引。只需使用.map((object, index) =&gt; object.bulkCreate(dtarr[index])) 【参考方案1】:

您可以使用 map 函数的 index 参数来获取 dtarr[index]

model.map((element, index) => element.bulkCreate(dtarr[index]));

【讨论】:

以上是关于如何将for循环转换为map函数的主要内容,如果未能解决你的问题,请参考以下文章