简体   繁体   English

如何使用逗号分隔的值从文本框中创建二维数组

[英]How to create a two-dimensional array from textbox with comma separated values

I have a html textbox with comma seperated values for example 我有一个带有逗号分隔值的html文本框,例如

name,age 姓名年龄
name,age 姓名年龄
name,age 姓名年龄

and so on. 等等。 The structure will always be the same. 结构将始终相同。 Now i want to create an array where i can retrieve a specified value like: 现在,我想创建一个数组,在其中我可以检索指定的值,例如:

array[1][0] (two-dimensional) 数组[1] [0](二维)

my code so far is: 到目前为止,我的代码是:

//retrieve values from textbox
var lines = $('#area').val().split(/\n/);

//create an array   
    var texts = []
    for (var i=0; i < lines.length; i++) {
        // only push this line if it contains a non whitespace character.
        if (/\S/.test(lines[i])) {
            texts.push($.trim(lines[i]));
        }
       if (/^[,]+$/.test(lines[i][i])) {
            texts.push($.trim(lines[i][i]));
        }

}

But this doesnt work.Does anyone have an idea how i can do this in javascript? 但这是行不通的。有没有人知道我如何在javascript中做到这一点? I am a beginner in Javascript :-) 我是Java语言的初学者:-)

The goal should be to get all inputed names as an array and the age as well. 目标应该是获取所有输入的名称作为数组以及年龄。

Thanks in advance!! 提前致谢!!

Change this: 更改此:

texts.push($.trim(lines[i]));

To this: 对此:

 texts.push($.trim(lines[i]).split(","));

Fiddle 小提琴

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

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