Array.prototype.map() expects a return value from arrow function
95 // 按字典序排序
96 keys = keys.sort();
97 const list = [];
98 keys.map((key) => { 99 list.push(`${key}${params[key]}`);
100 });
101 const contentStr = list.join('');
Description
Array
has several methods for filtering, mapping, and folding.
If we forget to write return statement in a callback of those, it's probably a mistake.
If you don't want to use a return
or don't need the returned results, consider using .forEach
instead.
Bad Practice
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {});
const foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
});
const bar = foo.filter(function(x) {
if (x) {
return true;
} else {
return;
}
});
Recommended
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
return memo;
}, {});
const foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
return false;
});
const bar = foo.map(node => node.getAttribute("id"));