我正在用lodash v3做一个项目,我有eslint-plugin-loadash设置来使用v3的建议。我有eslint-plugin-loadash的设置来使用v3的建议。
从下面的代码来看,它建议将它从 _.forEach
变成 _.map
. inventory.minons()
是一个对象数组。
var playerArmies = [setupPlayer];
_.forEach(inventory.minions(), function () {
playerArmies.push(setupSubCommander);
});
看了一下文件中的 _.map
我想不出来怎么把我的东西改成它建议的样子。我现在不太担心这样做是否正确(由于不懂,我是linter的奴隶),更多的只是想扩展自己的知识,希望能帮助我理解未来的文档。
因为你在数组中有一个初始值。_.map
不会保留该项目,你必须使用 .unshift
添加项目 setupPlayer
到数组的开始。
var playerArmies = _.map(inventory.minions(), function () {
return setupSubCommander;
});
playerArmies.unshift(setupPlayer); // adds item to the front of the array.