[英]Getting All Values from Comma Separated String in Javascript
I have 1 String Variable in JavaScript which contains 3 Comma separated values shown below: 我在JavaScript中有1个String变量,其中包含3个逗号分隔的值,如下所示:
var session = "mick@yahoo.com,123456,f_id=8";
I want to get all above 3 comma separated values in 3 different variables. 我想在3个不同的变量中获得3个以上逗号分隔的值。 What I have achieved so far is getting last comma separated value in elem2 variable and getting other 2 comma separated values 1st and 2nd one in elem1 variable. 到目前为止,我已经实现的是在elem2变量中获取最后一个逗号分隔值,并在elem1变量中获取其他两个逗号分隔值第一和第二个值。
var elem1 = session.split(",");
var elem2 = elem1.pop();
document.write("elem1 = "+elem1+"<br/>elem2 = "+elem2);
So my program is not working fine as I wants, please I need quick working solution so that I have another variable elem3 and I will get following output in my browser: 所以我的程序无法正常运行,请使用快速解决方案,以便使用另一个变量elem3,然后在浏览器中获取以下输出:
elem1 = mick@yahoo.com
elem2 = 123456
elem3 = f_id=8
You could try the following: 您可以尝试以下方法:
// Initially, we split are comma separated string value on commas.
// In the values an array of these values is stored.
var values = session.split(",");
// Then we get each value based on it's position in the array.
var email = values[0];
var number = values[1];
var id = values[2];
As a side note, I would suggest you use meaningful names for your variables like above. 作为附带说明,我建议您为变量使用有意义的名称,就像上面的那样。 The names elem1
, elem2
and elem3
are a bit vague and in future thses namings may obscure even you, let alone another reader of your code. 名称elem1
, elem2
和elem3
有点含糊,将来,这些命名甚至都可能使您难以理解,更不用说代码的另一个读者了。
You can also store the session data in a JavaScript Object . 您还可以将会话数据存储在JavaScript对象中 。 Below iterates through each split string and adds it to an Object. 下面遍历每个拆分的字符串,并将其添加到Object。
var myData = {};
// ...
for(var i = 0, x = session.split(","); i < x.length; i++) {
switch(i) {
case 0: myData.email = x[i]; break;
case 1: myData.number = x[i]; break;
case 2: myData.f_id = x[i]; break;
}
}
If you're dealing with multiple data values, it's best to make an array of objects, like so. 如果要处理多个数据值,最好像这样制作一个对象数组。
var myData = [];
// ...
myData.push({
email: x[0],
number: x[1],
f_id: x[2]
});
And so on. 等等。 This is a form of JSON Notation . 这是JSON符号的一种形式。 The output would look like this: 输出如下所示:
[
{
email: "mick@yahoo.com",
number: "123456",
f_id: "f_id=8"
},
{
// Another session, etc.
}
]
You could also use string.replace with a callback as parameter. 您也可以将string.replace与回调一起用作参数。 Very flexible! 非常灵活!
Check this for more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace 检查此以获得更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace
For your usecase you may end up with: 对于您的用例,您可能最终得到:
function writeParts(match, email, number, id, offset, string) {
document.write("email = "+email+"<br/>number = "+number);
}
// match not-comma followed by a comma (three times)
session.replace(/([^,]+),([^,]+),([^,]+)/, writeParts);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.