在 Javascript 中重复使用 array.map
using array.map with repeat in Javascript
您好,我有以下内容:
var a = [1, 2, 3]
我想将此数组更新为:
a = [11,22,33]
我正在尝试做类似
的事情
a.map(repeat(2));
但它会导致错误。
我知道我可以很容易地遍历这个,但我正在尝试使用更简洁的代码来练习,并在不同的函数以及如何使用它们方面扩展我的知识。
这样的事情可能吗?
鉴于您似乎在寻找重复的数字,请为此使用字符串。为此目的有一个方便的repeat
method:
a.map(x => Number(String(x).repeat(2))));
如果你想使用你的符号,你需要创建一个高阶 repeat
函数,returns 另一个函数用作 map
回调:
function repeat(times) {
return x => String(x).repeat(times);
}
a.map(repeat(2)).map(Number)
是的,这是可能的。您可以将 repeat
定义为 return 函数的函数:
function repeat(times) {
return function (value) {
return +String(value).repeat(times);
}
}
// Your code:
var a = [1, 2, 3];
var result = a.map(repeat(2));
console.log(result);
map
方法需要一个函数作为参数,因此对 repeat(2)
的调用应该 return 一个函数。该(内部)函数使用 String#repeat
after converting the value to string, and then converts the result back to number with the unary +
.
您可以将数字转换为字符串,重复该值并将其转换回数字。
var a = [1, 2, 3]
a = a.map(a => +a.toString().repeat(2));
console.log(a);
很简单,用Array join() method试试:
a.map(x => Array(2).join(x));
给你,number
版本和string
版本
const doubleIt = n => Number(`${n}${n}`);
const arr = [1, 2, 3, 55];
const strArr = arr.map(o => `${o}${o}`);
const numArr = arr.map(o => o * 11);
const numFromStringsArr = arr.map(o => doubleIt(o));
console.log(strArr);
console.log(numArr);
console.log(numFromStringsArr);
您可以使用 Array()
创建 N .length
的数组,使用 Array.prototype.fill()
用 .map()
回调的当前元素填充创建的数组,链 Array.prototype.join()
以 ""
作为参数,使用 +
运算符将字符串转换为数字
var a = [1, 2, 3], n = 2;
a = a.map(el => +Array(n).fill(el).join(""));
console.log(a);
您好,我有以下内容:
var a = [1, 2, 3]
我想将此数组更新为:
a = [11,22,33]
我正在尝试做类似
的事情a.map(repeat(2));
但它会导致错误。
我知道我可以很容易地遍历这个,但我正在尝试使用更简洁的代码来练习,并在不同的函数以及如何使用它们方面扩展我的知识。
这样的事情可能吗?
鉴于您似乎在寻找重复的数字,请为此使用字符串。为此目的有一个方便的repeat
method:
a.map(x => Number(String(x).repeat(2))));
如果你想使用你的符号,你需要创建一个高阶 repeat
函数,returns 另一个函数用作 map
回调:
function repeat(times) {
return x => String(x).repeat(times);
}
a.map(repeat(2)).map(Number)
是的,这是可能的。您可以将 repeat
定义为 return 函数的函数:
function repeat(times) {
return function (value) {
return +String(value).repeat(times);
}
}
// Your code:
var a = [1, 2, 3];
var result = a.map(repeat(2));
console.log(result);
map
方法需要一个函数作为参数,因此对 repeat(2)
的调用应该 return 一个函数。该(内部)函数使用 String#repeat
after converting the value to string, and then converts the result back to number with the unary +
.
您可以将数字转换为字符串,重复该值并将其转换回数字。
var a = [1, 2, 3]
a = a.map(a => +a.toString().repeat(2));
console.log(a);
很简单,用Array join() method试试:
a.map(x => Array(2).join(x));
给你,number
版本和string
版本
const doubleIt = n => Number(`${n}${n}`);
const arr = [1, 2, 3, 55];
const strArr = arr.map(o => `${o}${o}`);
const numArr = arr.map(o => o * 11);
const numFromStringsArr = arr.map(o => doubleIt(o));
console.log(strArr);
console.log(numArr);
console.log(numFromStringsArr);
您可以使用 Array()
创建 N .length
的数组,使用 Array.prototype.fill()
用 .map()
回调的当前元素填充创建的数组,链 Array.prototype.join()
以 ""
作为参数,使用 +
运算符将字符串转换为数字
var a = [1, 2, 3], n = 2;
a = a.map(el => +Array(n).fill(el).join(""));
console.log(a);