数据结构和函数

数组重排序方法:

reverse():反转数组项的顺序,注意使用该方法时会改变原来的数组顺序,而不是返回一个副本。

let arr = [1,2,3,4,5];

console.log(arr.reverse());//[ 5, 4, 3, 2, 1 ]

console.log(arr);//[ 5, 4, 3, 2, 1 ]

 

sort():按照升序排列数组每一项

let arr = [0,12,3,7,-12,23];

console.log(arr.sort());

//[ -12, 0, 12, 23, 3, 7 ]

调用sort()方法后排序没有正确。

原因在于sort()方法排序时首先会调用每个元素的toString()转型方法,然后得到比较的字符串。即使每项都是数值,sort()方法比较的也是字符串。

 

解决方法:sort()方法可以接收一个比较函数作为参数,以便我们指定哪个值位于哪个值前面。比较函数接收两个参数,如果第一个参数应该位于第二个数前面,返回一个负数,如果两个参数相等返回0,如果第一个参数应该位于第二个数的后面,返回一个正数。

let arr = [0,12,3,7,-12,23];

console.log(arr.sort(function(a,b){

    if(a < b){

        return -1;

    }else if(a > b){

        return 1;

    }else{

        return 0;

    }

}));

 

降序排列,只需要将返回值进行修改即可

 

let arr = [0,12,3,7,-12,23];

console.log(arr.sort(function(a,b){

    if(a < b){

        return 1;

    }else if(a > b){

        return -1;

    }else{

        return 0;

    }

}));

 

更简单的书写方式:

let arr = [0,12,3,7,-12,23];

console.log(arr.sort(function(a,b){

    return a - b;

    //????? b - a

}));

 

位置方法:

indexOf()lastIndexOf():这两个方法都是接收两个参数:要查找的项目和查找的起点位置索引。区别在于一个是从数组的开头开始找,一个是从数组末尾开始找,如果没有就返回-1

let arr = ["H","e","l","l","o"];

console.log(arr.indexOf("l"));//2

console.log(arr.lastIndexOf("l"));//3

console.log(arr.indexOf("z"));//-1

这两个方法查找时是采用的全等进行比较

let arr = ["1","2","3"];

console.log(arr.indexOf(1));//-1

 

includes():用于查找数组里面是否包含某个元素,包含返回true,否则返回false

let arr = ["1","2","3"];

console.log(arr.includes(2));//flase

console.log(arr.includes("2"));//true

console.log(arr.includes(7));//false

 

集合:

创建集合

let s1 = new Set();

let s2 = new Set([1,2,3]);

console.log(s1);//Set {}

console.log(s2);//Set { 1, 2, 3 }

 

使用add()方法可以给一个集合添加值

let s1 = new Set();

s1.add(1);

console.log(s1);//Set { 1 }

s1.add(2).add(3).add(4);

console.log(s1);

//Set { 1, 2, 3, 4 }

可以直接将一个数组传入add()方法里面

let s1 = new Set();

s1.add([1,2,3]);

console.log(s1);

//Set { [ 1, 2, 3 ] }

 

集合相关属性和方法

size属性获取元素个数

let s1 = new Set([1,2,3]);

console.log(s1.size);//3

 

has()方法来查看一个集合中是否包含某一个值

let s1 = new Set([1,2,3]);

console.log(s1.has(1));//true

console.log(s1.has("1"));//false

 

删除集合值

delete删除Set对象里面的某个元素

let s1 = new Set([1,2,3]);

s1.delete(2);

console.log(s1);//Set { 1, 3 }

//没有的元素也不会报错

s1.delete("2");

console.log(s1);//Set { 1, 3 }

一次性删除所以的元素,可以使用clear方法

let s1 = new Set([1,2,3]);

s1.clear()

console.log(s1);//Set {}

 

遍历集合

for-of

let s = new Set([1,2,3,4,5]);

for(let i of s){

    console.log(i);

}

// 1

// 2

// 3

// 4

// 5

 

forEarch

let s = new Set([1,2,3,4,5]);

s.forEach(ele => console.log(ele));

// 1

// 2

// 3

// 4

// 5

 

keys()方法遍历集合的键

let s = new Set(["Bill","Lucy","David"]);

for(let i of s.keys()){

    console.log(i);

}

// Bill

// Lucy

// David

 

values()方法遍历集合的值

let s = new Set(["Bill","Lucy","David"]);

for(let i of s.values()){

    console.log(i);

}

// Bill

// Lucy

// David

 

entries()方法同时遍历集合的键与值

let s = new Set(["Bill","Lucy","David"]);

for(let i of s.entries()){

    console.log(i);

}

// [ 'Bill', 'Bill' ]

// [ 'Lucy', 'Lucy' ]

// [ 'David', 'David' ]

 

集合转数组

let s1 = new Set([1,2,3]);

console.log(s1);//Set { 1, 2, 3 }

let arr = [...s1];

console.log(arr);//[ 1, 2, 3 ]

 

let s1 = new Set([1,2,3]);

console.log(s1);//Set { 1, 2, 3 }

let arr = Array.from(s1);

console.log(arr);//[ 1, 2, 3 ]

 

映射:

创造映射

let m = new Map();

m.set("name","xiejie");

m.set("age",18);

console.log(m);

//Map { 'name' => 'xiejie', 'age' => 18 }

console.log(m.get("name"));

//xiejie

 

let m = new Map();

m.set("name","xiejie");

m.set("age",18);

console.log(m);

//Map { 'name' => 'xiejie', 'age' => 18 }

console.log(m.get("name"));

//xiejie

 

函数:

函数声明:

字面量声明

function 函数名(形参){

    //函数体

}

 

function test(name){

    console.log("Hello,"+name);

}

test("xiejie");//Hello,xiejie

 

函数表达式声明函数

let 变量 = function(){

    //函数体

}

 

let test = function(name){

    console.log("Hello,"+name);

}

test("xiejie");//Hello,xiejie

 

函数调用

let test = function(){

    console.log("Hello");

}

let i = test;//

 i();//Hello

 

函数的返回值

let test = function(){

    return "Hello";

}

let i = test();

console.log(i);//Hello

 

let test = function(){

    let arr = [];

    for(let i=1;i<=60;i++)

    {

        if(i%10==7 || i%7==0)

        {

            arr.push(i);

        }

    }

    return arr;

}

console.log(test());

//[ 7, 14, 17, 21, 27, 28, 35, 37, 42, 47, 49, 56, 57 ]

函数的参数

 

参数名可以重复,同名的参数取最后一个参数值

function test(x,x){

    console.log(x);

}

test(3,5);//5

 

不定参数

function test(a,...b){

    console.log(a);//1

    console.log(b);//[2,3]

}

原文地址:https://www.cnblogs.com/boring333/p/11146249.html