获取数组的第一项 [英] Get first item of the array
本文介绍了获取数组的第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i used split() to a string "Haste, Haste, Please Haste, Please Haste to the Mountain" in an array. I have three variables v1, v2, v3 all string type.
I want the result as follows:
V3 = Please Haste to the Mountain
V2 = Please Haste
V1 = Haste, Haste ("Haste, " can be repeated in this substring)
I was able to retrieve V3 and v2 using jquery but getting a little stuck when it comes to V1.
My array should be like this:
array[0] array[1] array[2]
------------------------------------------------------------------------------
Haste, Haste, Haste,......Haste | Please Haste | Please Haste To The Mountain |
------------------------------------------------------------------------------
Please help
。
推荐答案
var str = "Haste,Please Haste, Please Haste to the Mountain"
var arr = str.split(',');
var V3, V2, V1 = '';
V3 = arr[3];
V2 = arr[2];
for (var i = 0; i < 10; i++) {
if (i == 0) { V1 += arr[0]; }
else if (i > 1 && i < 3) { V1 += ',' + arr[0]; }
else if (i >= 3 && i < 9) { V1 += '.' }
else {V1+= arr[0] }
}
试试这段代码
Try this code
var str = "Haste,Please Haste, Please Haste to the Mountain"
var arr = str.split(',');
var v1="",v2,v3,temp="";
arr.foreach(function(item){
if(item=="Haste"){
v1=v1+","+item;
}
else{
temp=temp+","+item;
}
});
var arr2=temp.split(',');
v2=temp[0];
v3=temp[1];
这篇关于获取数组的第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文