使用逗号分隔的数组,JavaScript的 [英] separating an array with commas, javascript

查看:145
本文介绍了使用逗号分隔的数组,JavaScript的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的场景。我有一个可以有多重选择的组合框。我的数据可能包含逗号。我使用Infragistics的控制,所以我不分离器中的控制权。现在,我的组合框的值是A * A,A%A和A,A。如果用户选择全部3个值,结果是这样的:

I have a strange scenario. I have a combo box that can have multiple selections. My data may contain commas. I'm using Infragistics controls so i'm not in control of the separator. Now, my combo box values are "A*A" "A%A" and "A,A". If the user selects all 3 values, the result looks like this:

    var arrValues = combo.igCombo('values'); //this retrieves the values            
    //arrValues contains this now: [A*A,A&A,A,A]

我用逗号解析的价值观和你猜对了,我有4个数组:

I'm parsing the values by a comma and you guessed it, i have an array of 4:

            array[0] = 'A*A'
             array[1] = 'A&A'
           array[2] = 'A' //the parse split "A,A" into 2 values
          array[3] = 'A'

数组[2]应

          array[2] = 'A,A'

和阵列[3]应该不存在。我有一个功能,我以为会工作,但事实并非如此。

and array[3] shouldn't exist. I have a function i thought would work but it doesn't.

      function getComboBoxValues(combo, delim, index) {
           var arrValues = combo.igCombo('values');
            var values = "";
           $.each(arrValues, function (i, o) {
               values += ((delim) ? o.split(delim)[index || 0] : o) + ',';
             }
           );
       return values;
    }

我把它称为是这样的:

I call it like this:

  var array = getComboBoxValues($('#mycombobox'), ',', 0);

我怎么能解析我的字符串:

How can i parse my string:

[A * A,A和A,A,A]

[A*A,A&A,A,A]

到一个数组所以它只有3个值

into an array so it will have only 3 values

            array[0] = 'A*A'
             array[1] = 'A&A'
           array[2] = 'A,A'

请记住,这可能不是在该字符串将是顺序,因此计数逗号将无法工作。

Keep in mind, this may not be the order in which the string will be, so counting commas won't work.

我的服务器端code是C#,所以如果这可以在C#来实现,请让我知道。谢谢

My server side code is C#, so if this can be achieved in C#, please let me know. Thanks

推荐答案

看着 troycomptonenterprises.com 并执行 combo.igCombo('值'); 给你回包含在一个字符串的值(包括问题的值)的数组。没有什么错在这里。

Looking at troycomptonenterprises.com and executing combo.igCombo('values'); gives you back an array containing the values (including the problem value) in a string. Nothing wrong here.

输出是:

 array[0] = "A*A"
 array[1] = "A&A"
 array[2] = "A,A"

阅读你的问题是你想要什么。

Reading your question that is exactly what you want.

这篇关于使用逗号分隔的数组,JavaScript的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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