javascript为自己将数组元素转换为数组 [英] javascript convert elements of an array into array for themselves

查看:53
本文介绍了javascript为自己将数组元素转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将数组元素转换为另一个数组?

How can I convert the elements of the array into another array?

let a = [2, 4, 0, 8, 9, 15]

,结果将是:

a = [[2], [4], [0], [8], [9], [15]]


推荐答案

使用 Array#map 迭代数组,并将每个项目包装在一个数组中:

Use Array#map to iterate the array, and wrap each item in an array:

let a = [2, 4, 0, 8, 9, 15]

const result = a.map((n) => [n])

console.log(JSON.stringify(result))

这篇关于javascript为自己将数组元素转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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