简体   繁体   English

Javascript:使用2个值分隔字符串

[英]Javascript: Separate a string with 2 values

I'm pretty new to javascript and i have following problem: 我对javascript很陌生,并且遇到以下问题:

in an alert popup i'm getting back a variable with 2 values which are separated by ",". 在警报弹出窗口中,我返回了一个带有2个值的变量,这些值由“,”分隔。

alert example: 1,2 警报示例:1,2

now i want to get this values separated so i get 2 different variables 现在我想分开这个值,所以我得到2个不同的变量

value 1 = 1 值1 = 1

value 2 = 2 值2 = 2

when there is no value alert example: ,2 then it should look like this: value 1 = 0 value 2 = 2 当没有值警报示例:,2时,应如下所示:值1 = 0值2 = 2

Use split which will split the string into array using mentioned separator 使用split将使用提到的separator将字符串拆分为array

expr1 || expr2 expr1 || expr2 => Returns expr1 if it can be converted to true; expr1 || expr2 =>如果可以将其转换为true,则返回expr1;否则返回false。 otherwise, returns expr2. 否则,返回expr2。 Thus, when used with Boolean values, || 因此,当与布尔值一起使用时,|| returns true if either operand is true; 如果任一操作数为true,则返回true; if both are false, returns false. 如果两者均为假,则返回false。

 var data = ',2'; var splitted = data.split(','); alert(splitted[0] || 0); alert(splitted[1] || 0); 

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

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