Some methods of JS
indexOf
var str='abcdgdjfahgzk'
console.log(str.indexOf('g'));//4
console.log(str.indexOf('l'));//-1
Whether the match has the same character, yes, return its position, no, return -1
join
join turns an array into a string
var str2=new Array();
str2=[5,4789,4,9,4,9,4,9,4,94]
console.log(str2.join(''));//54789494949494括号内为分隔条件,这里分隔为空
pop
pop() removes the last element from the array and returns it
var str2=new Array();
str2=[5,4789,4,9,4,9,4,9,4,94]
console.log(str2.pop());//94
console.log(str2);//[5, 4789, 4, 9, 4, 9, 4, 9, 4]
shift
shift() removes the first element from the array and returns that element
var str2=new Array();
str2=[5,4789,4,9,4,9,4,9,4,94]
console.log(str2.shift());//5
console.log(str2);//[4789, 4, 9, 4, 9, 4, 9, 4]
unshift
unshift() appends an element to the first index position in the array
str2.unshift(1);
console.log(str2)//[1, 5, 4789, 4, 9, 4, 9, 4, 9, 4, 94]
for in
var ty={
'name':'张三',
'age':'20'
}
for(var items in ty){
console.log(items)//name,age
}
for Each
for Each traverses the array and returns two parameters, one content and one index
str2.forEach(function(items,index){
console.log(items,index)
}
map
Creates a new array whose result is the result returned by calling a provided function on each element in the array, extremely fast,
The return result is true and false
var newarr=str2.map(function(item,index){
return item<10;
})
console.log(newarr)
//10) [true, false, true, true, true, true, true, true, true, false]
delete
delete arr2[1]//不常用
//splice(index,deleteElementlenght,ele1,ele2,ele3);
var arr4=['I','LOVE','JAVAscript','caigui']
var arr5=arr4.splice(1,2,'get','change')//arr4中的1,2位置返回给arr5,用后面两个代替
//split()将字符串转换为数组
//slice()返回一个新的数组对象,左开右闭
sort
Sorting, if a is less than b, that is, a - b is less than zero, then return a value less than zero, and the array will be sorted in ascending order.
Returns 0 if a is equal to b.
If a is greater than b, that is, a - b is greater than zero, then return a value greater than zero, and the array will be sorted in descending order.
var arr = ['General','Tom','Bob','John','Army'];
var resArr = arr.sort();
console.log(resArr);//输出 ["Army", "Bob", "General", "John", "Tom"]
var arr2 = [30,10,111,35,1899,50,45];
var resArr2 = arr2.sort();
console.log(resArr2);//输出 [10, 111, 1899, 30, 35, 45, 50]
//不传参,按照字符编码的顺序排序
ar arr3 = [30,10,111,35,1899,50,45];
arr3.sort(function(a,b){
return a - b;//a小于b,升序
})
console.log(arr3);//输出 [10, 30, 35, 45, 50, 111, 1899]
var arr4 = [30,10,111,35,1899,50,45];
arr4.sort(function(a,b){
return b - a;//a大于b,降序
})
console.log(arr4);//输出 [1899, 111, 50, 45, 35, 30, 10]