1.join 数组变成字符串 不改变原数组
1 let arr1 = ['I', 'Love', 'You'] 2 console.log(arr1.join(), arr1);//I,Love,You, ['I', 'Love', 'You'] 3 console.log(arr1.join('-'), arr1);//I-Love-You ['I', 'Love', 'You']2.split 字符串变成数组 不改变原数组
1 let string1 = 'I/Love/You' 2 console.log(string1.split('/'), string1); //['I', 'Love', 'You'] 'I/Love/You'3.push 末尾添加 改变原数组 4.pop 末尾删除 改变原数组 5.unshift 首位添加 改变原数组 6.shift 首位删除 改变原数组
1 console.log(arr1.push('ha'),arr1);//4 ['I', 'Love', 'You', 'ha'] 2 console.log(arr1.pop('ha'),arr1);//ha ['I', 'Love', 'You'] 3 console.log(arr1.unshift('ha'),arr1);//4 ['ha', 'I', 'Love', 'You'] 4 console.log(arr1.shift('ha'),arr1);//ha ['I', 'Love', 'You']7.reverse 反转数组 改变原数组
1 console.log(arr1.reverse(), arr1);//["You","Love","I"] ["You","Love","I"]8.sort 排序 改变原数组
1 let arr2 = [3, 9, 2, 90, 34] 2 console.log(arr2, arr2.sort((a, b) => { return a - b }));//[90,34,9,3,2] [90,34,9,3,2] 3 console.log(arr2, arr2.sort((a, b) => { return b - a }));//[2,3,9,34,90] [2,3,9,34,90]9.concat 数组拼接 不会改变原数组
1 console.log(arr1.concat(arr2),arr1,arr2);// ['You', 'Love', 'I', 90, 34, 9, 3, 2] ['You', 'Love', 'I'] [90, 34, 9, 3, 2]10.splice 截取/删除替换数组 改变原数组 11.slice 截取/删除数组 不会改变原数组
1 let arr3=[1,2,3,4,5,6,7] 2 let arr4=[1,2,3,4,5,6,7] 3 let arr5=[1,2,3,4,5,6,7] 4 console.log(arr3.splice(0,3),arr3);//[1, 2, 3] [4, 5, 6, 7] 5 console.log(arr4.splice(0,3,'嘿嘿'),arr4);//[1, 2, 3] ['嘿嘿', 4, 5, 6, 7] 6 console.log(arr5.slice(1,3),arr5);//[2, 3] [1, 2, 3, 4, 5, 6, 7]
标签:常用,console,数组,JS,arr1,Love,ha,log From: https://www.cnblogs.com/tyysf/p/17090033.html