用 reduce 实现 map
代码
js
function implementMapUsingReduce(list, func) {
return list.reduce((acc, cur, i) => {
acc[i] = func(cur);
return acc;
}, []);
}
const a = implementMapUsingReduce([1, 2, 3, 4], a => a + 1); // [2,3,4,5]
console.log(a);
const b = implementMapUsingReduce(["a", "b", "c"], a => a + "!"); // ['a!', 'b!', 'c!']
console.log(b);
function implementMapUsingReduce(list, func) {
return list.reduce((acc, cur, i) => {
acc[i] = func(cur);
return acc;
}, []);
}
const a = implementMapUsingReduce([1, 2, 3, 4], a => a + 1); // [2,3,4,5]
console.log(a);
const b = implementMapUsingReduce(["a", "b", "c"], a => a + "!"); // ['a!', 'b!', 'c!']
console.log(b);