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

时间:2022-01-25 19:43:03

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.

我需要这种格式的输入,所以我可以向db发送多个值。

How can I do this elegantly?

我怎么能优雅地做到这一点?

1 个解决方案

#1


3  

I can't think of anything more elegant for this than map:

我想不出比地图更优雅的东西:

E.g.:

例如。:

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]);

#1


3  

I can't think of anything more elegant for this than map:

我想不出比地图更优雅的东西:

E.g.:

例如。:

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]);