数组常用方法
var arr1 = ['hello', 'world'];
修改数组值(修改原数组)
push/pop/shift/unshift
push:向末尾添加值
arr1.push('test'); // ["hello", "world", "test"]
pop:删除数组最后一项
arr1.pop(); // ["hello", "world"]
console.log(arr1.pop()) // world
//note: pop()输出数组最后一项,原数组删除最后一个元素
shift:删除数组第一项
arr1.shift(); // ["world"]
unshift:向数组首部添加一个值或多个值
arr1.unshift('xixi', 'hello'); // ["xixi", "hello", "world"]
splice修改原数组
删除:从索引为0处删除一个数
arr1.splice(0, 1); // ["hello", "world"]
增加:从索引为1处删除0个,增加字符串
arr1.splice(1, 0, ' '); // ["hello", " ", "world"]
替换:从索引为1处 删除1个数,增加字符串,即替换
arr1.splice(1, 1, 'splice'); //["hello", "splice", "world"]
reverse翻转数组, 修改原数组
arr1.reverse() // ["world", "splice", "hello"]
sort排序方法,修改原数组
数字从小到大排序
var arr6 = ['2', '1', '3', '8', '4', '6', '5', '7']; console.log(arr6.sort()); //["1", "2", "3", "4", "5", "6", "7", "8"]
字符串从a到e排序
var arr7 = ['boy', 'alice', 'dad', 'wc', 'ele'] console.log(arr7.sort()); //["alice", "boy", "dad", "ele", "wc"]
时间从小到大排序
var arr8 = ['1996-09-29', '1995-01-01', '2020-7-01', '1010-07-01'] console.log(arr8.sort()); // ["1010-07-01", "1995-01-01", "1996-09-29", "2020-7-01"]
返回数组元素(不影响原数组)
^(* ̄(oo) ̄)^:slice/split/concat/join这几种方法返回一个新的数组,不影响原数组
slice
语法:返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素
返回从1开始之后所有的元素
console.log(arr1.slice(1), arr1); //["splice", "hello"]["world", "splice", "hello"]
返回第一个元素
console.log(arr1.slice(0,1), arr1); //["world"]["world", "splice", "hello"]
返回最后一个元素
console.log( arr1.slice(-1), arr1); //["world"] ["hello", "splice", "world"]
返回最后两个元素
console.log( arr1.slice(-2), arr1);// ["splice", "world"] ["hello", "splice", "world"]
返回从0到1的元素
console.log( arr1.slice(0, 2), arr1); //["hello", "splice"] ["hello", "splice", "world"]
split 自定义分割字符串,返回数组
var str2 = 'h:e:l:l:o'; console.log(str2.split(':'), str2); //["h", "e", "l", "l", "o"] h:e:l:l:o
var str1 = 'h-e-l-l-o w-o-r-d-l!!!' console.log(str1.split('-'), str1); //["h", "e", "l", "l", "o w", "o", "r", "d", "l!!!"] h-e-l-l-o w-o-r-d-l!!!
数组连接
concat
concat 连接两个或多个数组,不影响原数组,返回一个新的数组
var arr2 = ['hello', 'you']; var arr3 = ['are', 'beauty']; var arr4 = ['test']; console.log( arr4.concat(arr2, arr3)); // ["test", "hello", "you", "are", "beauty"] console.log(arr2,arr3,arr4); // ["hello", "you"]["are", "beauty"]["test"]
join
join 对于数组内部元素进行拼接,返回字符串,不影响原数组
var arr5 = new Array(3) arr5[0] = "George" arr5[1] = "John" arr5[2] = "Thomas" console.log(arr5.join('.'), arr5); // George.John.Thomas ["George", "John", "Thomas"]
数组去重
推荐:console.log([...new Set(arr)])
<!DOCTYPE html> <html> <head> <meta charset=' utf-8'> <meta name='author' content='http://www.softwhy.com/' /> <title></title> </head> <body> </body> <script> var arr = [1,2,3,1,5,2,0] //ES6 Set去重 // 法1 // function removeRepetition(array){ // return Array.from(new Set(array)) //类数组转化为数组, // } function removeRepetition(array){ for(var i=0;i<array.length;i++){ for(var j=i+1;j<array.length;j++){ if(array[i] === array[j]){ array.splice(j,1) j-- } } return array; } } // 法2 function setFun(arr){ // .new Set方法,返回是一个类数组,需要结合 ...运算符,转成真实数组 return [...new Set(arr)] } // 法3 function setFun(arr){ // .new Set方法,返回是一个类数组,需要结合 Array.from ,转成真实数组 return (Array.from(new Set(arr)) ) } console.log([...new Set(arr)]) // </script> </html>
数组的循环遍历方式
indexOf
作用:
查看字符串是否含有某元素,有则返回下标,没有则返回-1
('str').indexOf('y') // -1
('str').indexOf('s') // 0
for-i
var arr = [{'a':1},{'b':2},3]
for (var i = 0; i < arr.length; i++) {
console.info(arr[i]) // {a: 1} {b: 2} 3
}
each
var arr = [{'a':1},{'b':2},3]
arr.forEach(item => {
console.info('item', item) //// {a: 1} {b: 2} 3
})
$(arr).each((index,item)=>{
console.info('i', index, item)
})
in
var arr = [{ 'a': 1 }, { 'b': 2 }, 3]
for (keys in arr) {
console.log('for-in', keys) // 0 1 2
console.log('for-in', arr[keys]) // {a: 1} {b: 2} 3
}
of
var arr = [{ 'a': 1 }, { 'b': 2 }, 3]
for (let a of arr) {
console.log('for-of', a) //{a: 1} {b: 2} 3
}
方法:
Array.from(imgs).forEach(function (el) {
if (isIn(el)) {
。。。
}
})
[]可以代替.操作, 对象后面的.操作可以用[]代替
var oBox=document.getElementById('box'); oBox.innerHTML='123'; oBox['innerHTML']='456'; oBox['innerText']='<p>scascd</p>'; var a='innerHTML'; oBox[a]='<p>scascd</p>';
数组可以存所有数据类型
var a=5; var arr=['lee','jack',18,true,a];//可以存变量 var arr2=[ 1,arr,123, [1, function(){alert(1);} ] ] ; arr[2]=20; arr[5]=15; //arr[4]未定义,不占内存,开辟一个空间 arr[6]='' //空字符

浙公网安备 33010602011771号