简体   繁体   English

将逗号分隔值的字符串转换为多维javascript数组?

[英]converting a string of comma seperated values to a multidimensional javascript array?

In my getOptions.jsp file, I have created a string with jstl. 在我的getOptions.jsp文件中,我用jstl创建了一个字符串。

<c:set var="options" value="Maximize,Redo,RemoveFormat,Save" />

This string should now be transformed into this format (which is, if I am correct, a multidimensional javascript array (or is it json?)). 现在应该将此字符串转换为这种格式(如果我正确的话,这是一个多维javascript数组(或者是json?))。

[['Maximize', 'Redo','RemoveFormat','Save']] 

I have to do this because this format is expected by a javascript method located in another .jsp (showToolbar.jsp), 我必须这样做,因为位于另一个.jsp(showToolbar.jsp)中的javascript方法应采用这种格式,

function handleToolbar(options) {
          //toolbar = [['Maximize', 'Redo','RemoveFormat','Save']];  the expected format!
          toolbar = options;
    }

So my question is, how do I transform the comma-seperated string 'options' into the format that is expected in the 'toolbar' variable in order to pass it from one jsp to the other and pass it as a parameter of the handleToolbar function. 所以我的问题是,如何将逗号分隔的字符串'options'转换为'toolbar'变量中期望的格式,以便将其从一个jsp传递到另一个jsp并将其作为handleToolbar函数的参数传递。

Note: I guess it is not ideal to use javascript in a jsp but that I cannot change because I inherited the code. 注意:我想在jsp中使用javascript是不理想的,但是我不能更改,因为我继承了代码。

Thanks alot in advance, I've been searching for hours for the solution and can't find it. 在此先感谢您,我一直在寻找解决方案的数小时,找不到它。

Simple: 简单:

var myString = "Maximize,Redo,RemoveFormat,Save";
var myArray = [myString.split(',')];

The result is an array that has 1 element in it. 结果是其中包含1个元素的数组。 That 1 element is this array: 那1个元素就是这个数组:

['Maximize', 'Redo','RemoveFormat','Save']

So, myArray is: 因此, myArray为:

[['Maximize', 'Redo','RemoveFormat','Save']]

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

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