split method and splice method

1. split method

The split() method is used to split a string into an array of strings.
For example, there is a result of 128b8f730592cc8db33ea52985127d44,44bee6555822d8321d2d1a2c1ac3b2cf,b2f939f26e512934e165f3e784cc74ca in the data.
I need to turn this string into an array

			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]

The split method ends with',', and returns an array, which is what we want at the moment.

For example:

"2:3:4:5".split(":")	//Will return ["2", "3", "4", "5"]
"|a|b|c".split("|")	//Will return ["," "a," "b," "c"]

2. splice () method

The splice() method adds / deletes items to / from the array, and then returns the deleted items.

arrayObject.splice(index,howmany,item1,...,itemX)

The index must be filled in. Integer, which specifies the location of items to be added or deleted, and negative numbers can be used to specify the location at the end of the array.
How many must be filled in. Number of items to delete. If set to 0, the item will not be deleted.
item1,... itemX is optional. New items added to the array.

When howmany is zero

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

How many is the time of 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

How many is 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

Posted by uknowho008 on Tue, 01 Oct 2019 09:59:01 -0700