前端小知识

前端小知识Day5

2022-10-17  本文已影响0人  xsmile21

1、数组快速去重

// 此方法仅适用于基本数据类型组成的数组
const arr = [1, 2, 3, 4, 5, 6];
const arr2 = new Set(arr);
const arr3 = [...arr2];

2、数组一些常用的方法

let arr = [1, 2, 3];
arr.push(4, 5);
console.log(arr);    //  (5) [1, 2, 3, 4, 5]
let arr = [1, 2, 3];
arr.unshift(4, 5);
console.log(arr);    //  (5) [4, 5, 1, 2, 3]
let arr = [1, 2, 3, 4, 5];
arr.pop();
console.log(arr);    //  (4) [1, 2, 3, 4]
let arr = [1, 2, 3, 4, 5];
arr.shift();
console.log(arr);    //  (4) [2, 3, 4, 5]
let arr = [1, 2, 3, 4, 5];
arr.splice(1, 1, "2");
console.log(arr);    //  (5) [1, "2", 3, 4, 5]
let arr=[1, 2, 3, 4, 5];
arr.splice(1, 1);
console.log(arr);    //  (4) [1, 3, 4, 5]
let arr = [1, 2, 3, 4, 5];
console.log(arr.slice(2));   //  (3) [3, 4, 5]
let arr = [1, 2, 3, 4, 5];
console.log(arr.slice(2, 4));    //  (2) [3, 4]
let arr = [1, 2, 3, 4, 5];
console.log(arr.join("+"));    //  1+2+3+4+5
let arr = [2, 1, 3, 5, 4];
console.log(arr.sort());    //  (5) [1, 2, 3, 4, 5]
let arr = ["Mike", "Doinb", "Uzi", "Mary", "Faker", "ClearLove"];
console.log(arr.sort())    //  (6) ["ClearLove", "Doinb", "Faker", "Mary", "Mike", "Uzi"]
let arr = [1, 2, 3, 4, 5];
console.log(arr.reverse());    //  (5) [5, 4, 3, 2, 1]
let arr1 = ["RNG", "EDG"];
let arr2 = ["Uzi", "ClearLove"];
let arr3 = ["TOP", "SKT"];;
console.log(arr1.concat(arr2, arr3));    //  (6) ["RNG", "EDG", "Uzi", "ClearLove", "TOP", "SKT"]
 let arr=[1, 2, 3, 4, 5];
console.log(arr.toString());    //  1,2,3,4,5
上一篇 下一篇

猜你喜欢

热点阅读