wumi_blog
wumi_blog copied to clipboard
js splice方法split方法,slice方法 img没有src属性时就没有那个默认的图片图标(图挂了的那个)
js splice
splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。
语法:arrayObject.splice(index,howmany,item1,.....,itemX)
参数 | 描述 |
---|---|
index | 必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。 |
howmany | 必需。要删除的项目数量。如果设置为 0,则不会删除项目。 |
item1, ..., itemX | 可选。向数组添加的新项目。 |
返回值
类型 | 描述 |
---|---|
Array | 包含被删除项目的新数组,如果有的话。 |
注意:该方法会改变原始数组。
如果从 arrayObject 中删除了元素,则返回的是含有被删除的元素的数组。
//subAddresses变为 addressList从index位置删掉的项组成的数组(这里就删了一个,所以subAddresses为只有一项的数组)
//addressList被修改了
let subAddresses=addressList.splice(index,1);
Examples
var myFish = ['angel', 'clown', 'mandarin', 'surgeon'];
// removes 0 elements from index 2, and inserts 'drum'
var removed = myFish.splice(2, 0, 'drum');
// myFish is ['angel', 'clown', 'drum', 'mandarin', 'surgeon']
// removed is [], no elements removed
// myFish is ['angel', 'clown', 'drum', 'mandarin', 'surgeon']
// removes 1 element from index 3
removed = myFish.splice(3, 1);
// myFish is ['angel', 'clown', 'drum', 'surgeon']
// removed is ['mandarin']
// myFish is ['angel', 'clown', 'drum', 'surgeon']
// removes 1 element from index 2, and inserts 'trumpet'
removed = myFish.splice(2, 1, 'trumpet');
// myFish is ['angel', 'clown', 'trumpet', 'surgeon']
// removed is ['drum']
// myFish is ['angel', 'clown', 'trumpet', 'surgeon']
// removes 2 elements from index 0, and inserts 'parrot', 'anemone' and 'blue'
removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue');
// myFish is ['parrot', 'anemone', 'blue', 'trumpet', 'surgeon']
// removed is ['angel', 'clown']
// myFish is ['parrot', 'anemone', 'blue', 'trumpet', 'surgeon']
// removes 2 elements from index 2
removed = myFish.splice(myFish.length -3, 2);
// myFish is ['parrot', 'anemone', 'surgeon']
// removed is ['blue', 'trumpet']
js split方法
split() 方法用于把一个字符串分割成字符串数组。
stringObject.split(separator,howmany)
参数 | 描述 |
---|---|
separator | 必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。 |
howmany | 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。 |
注意:如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间都会被分割。""与" "是不同的
注释:String.split() 执行的操作与 Array.join 执行的操作是相反的。
此外
-
img没有src属性时就没有那个默认的图片图标(图挂了的那个) a标签没有href时(没有默认链接的样式)
-
=> 箭头函数没有自己的this
在写Promise的
.then()
回调时 总想着let that = this;
一下,好把this转存一下方便使用,其实then(function(resp){})
换成then((resp)=>{})
这种箭头函数就不需要使用that了 可以直接使用this(吐槽一下webstorm,js也设置成了es6了不知为何有时候写箭头函数就会报错...)
js slice
slice是指定在一个数组中的元素创建一个新的数组,即原数组不会变 The slice() method returns a shallow copy of a portion of an array into a new array object.
Syntax
arr.slice([begin[, end]])
Parameters
begin
Zero-based index at which to begin extraction. As a negative index, begin indicates an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence. If begin is undefined, slice begins from index 0.
end
Zero-based index at which to end extraction. slice extracts up to but not including end. slice(1,4) extracts the second element through the fourth element (elements indexed 1, 2, and 3). As a negative index, end indicates an offset from the end of the sequence. slice(2,-1) extracts the third element through the second-to-last element in the sequence. If end is omitted, slice extracts through the end of the sequence (arr.length).
Return a portion of an existing array
var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
var citrus = fruits.slice(1, 3);
// citrus contains ['Orange','Lemon']