1,split方法
split() 方法用于把一个字符串分割成字符串数组。
例如:数据中现在有这样一个结果 128b8f730592cc8db33ea52985127d44,44bee6555822d8321d2d1a2c1ac3b2cf,b2f939f26e512934e165f3e784cc74ca,
我需要把这个字符串变成数组
console.log(res.result.productImgIds)
//128b8f730592cc8db33ea52985127d44,44bee6555822d8321d2d1a2c1ac3b2cf,b2f939f26e512934e165f3e784cc74ca,
this.productImgList = res.result.productImgIds.split(',')
console.log(this.productImgList)
//(4) ["128b8f730592cc8db33ea52985127d44", "44bee6555822d8321d2d1a2c1ac3b2cf", "b2f939f26e512934e165f3e784cc74ca", "", __ob__: Observer]
this.productImgList.splice((this.productImgList.length-1),1)
console.log(this.productImgList)
//(3) ["128b8f730592cc8db33ea52985127d44", "44bee6555822d8321d2d1a2c1ac3b2cf", "b2f939f26e512934e165f3e784cc74ca", __ob__: Observer]
在split方法中以 ‘,’结尾的就会分割,返回一个数组,这个数组目前就是我们想要的
例如:
"2:3:4:5".split(":") //将返回["2", "3", "4", "5"]
"|a|b|c".split("|") //将返回["", "a", "b", "c"]
2,splice() 方法
splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。
arrayObject.splice(index,howmany,item1,…,itemX)
index 必需要填写的。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。
howmany 必需要填写的。要删除的项目数量。如果设置为 0,则不会删除项目。
item1, …, itemX 可选。向数组添加的新项目。
howmany 为0 的时候
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
document.write(arr + "<br />")
arr.splice(2,0,"William")
document.write(arr + "<br />")
George,John,Thomas,James,Adrew,Martin
George,John,William,Thomas,James,Adrew,Martin
howmany 为1 的时候
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
document.write(arr + "<br />")
arr.splice(2,1,"William")
document.write(arr)
George,John,Thomas,James,Adrew,Martin
George,John,William,James,Adrew,Martin
howmany 为3 的时候
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
document.write(arr + "<br />")
arr.splice(2,3,"William")
document.write(arr)
George,John,Thomas,James,Adrew,Martin
George,John,William,Martin