在Javascript中使用带有重复的array.map [英] using array.map with repeat in Javascript

查看:52
本文介绍了在Javascript中使用带有重复的array.map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有以下几点:

var a = [1, 2, 3]

我想将此数组更新为:

a = [11,22,33]

我正在尝试做类似的事情

I am trying to do something like

a.map(repeat(2)); 

但它会导致错误.

我知道我可以轻松地遍历这个,但我正在尝试使用更简洁的代码来练习,并扩展我对不同功能以及如何使用它们的知识.

I know I could easily loop through this, but I am trying to practice using more concise code, and expand my knowledge a bit on different functions, and how to use them.

这样的事情有可能吗?

推荐答案

您可以将数字转换为字符串,重复该值并将其转换回数字.

You could convert the number to string, repeat the value and convert it back to number.

var a = [1, 2, 3]
a = a.map(a => +a.toString().repeat(2));

console.log(a);

这篇关于在Javascript中使用带有重复的array.map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆