简体   繁体   English

如何使数组中的每个元素成为另一个数组?

[英]How do I make each each element in array another array?

I have some text that I receive from the user: 我收到了一些来自用户的文本:

var text = ['Hello', 'World']; // this is the product of string.split(',')

I need to convert it into an array like this one: 我需要将其转换成这样的数组:

var argument = [['Hello'], ['World']];

I need the input in this format so I can send multiple values to the db. 我需要这种格式的输入,以便可以将多个值发送到数据库。

How can I do this elegantly ? 我该如何优雅地做到这一点?

I can't think of anything more elegant for this than map : 我想不到有什么比map更优雅的了:

Eg: 例如:

var argument = originalString.split(",").map(function(entry) {
    return [entry];
});

Or if you've enabled ES6 on your NodeJS installation: 或者,如果您已在NodeJS安装上启用了ES6:

var argument = originalString.split(",").map((entry) => [entry]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM