天天看点

Array 总结

let arr1 = [];
console.log(arr1.push(1));// 1
arr1.push(true);
arr1.push({});
arr1.push(null);
arr1.push('name');
arr1.push(8.0,.8);
arr1.push(NaN);
console.log(arr1.push(undefined));// 9

let arr2 = [1, true, {}, null,'name',8.0,.8,NaN,undefined];

console.log(arr1);//[ 1, true, {}, null, 'name', 8, 0.8, NaN, undefined ]
console.log(arr2);//[ 1, true, {}, null, 'name', 8, 0.8, NaN, undefined ]
// 结果说明: push函数的结果是将一个元素放入队尾
// 结果说明: push可以多个值
// 结果说明: push的返回值是: push完成后,新数组的长度
// 结果说明: 可以存放任意类型数据, 元素类型可以不同

// 判断是否是数组
// arr1.isArray()  -- error 没有isArray
console.log( typeof arr2, typeof {} )// object object
console.log(arr1 instanceof Array)// true
console.log(arr2 instanceof Object)// true
console.log({} instanceof Array)// false
console.log({} instanceof Object)// true
// 结果说明: 判断是否是数组使用: arr instanceof Array

// 计算长度
console.log('arr1.length = ',arr1.length);//
console.log('arr2.length = ',arr2.length);//

// 判断相等
// 数组严格模式, 每一项必须相等,比较的是引用
console.log( 'arr1 == arr2 ',arr1 == arr2 );// false
console.log( 'arr1 === arr2 ',arr1 === arr2 );// false
console.log( 'Object.is(arr1,arr2)',Object.is(arr1,arr2) );// false
console.log( 'JSON.stringify(arr1) === JSON.stringify(arr2)',
        JSON.stringify(arr1) === JSON.stringify(arr2));// true

// 元素严格模式, 每一项必须相等
function compareArr(arr1,arr2){
    if(arr1.length === arr2.length){
        for (let i=0; i<arr1.length; i++){
            if( arr1[i] !== arr2[i]){
                return false;
            }
        }
        return true;
    }
    return false;
}
console.log( 'compareArr(arr1,arr2)',compareArr(arr1,arr2));// false

console.log('\n转成json')
console.log('JSON.stringify(arr1)',JSON.stringify(arr1));
// 结果说明null, undefined, 都会转成'null'

console.log('\ntoString(),toLocaleString()使用 ');
console.log(arr1.toString());//1,true,[object Object],,name,8,0.8,NaN
console.log(arr1.toLocaleString());//1,true,[object Object],,name,8,0.8,NaN
// 结果说明null, undefined, 都会转成 '', 即空字符串

// join 使用.  元素连接
console.log('\njoin() 使用. ');
console.log(arr1.join("")); //1true[object Object]hello80.8NaN
console.log(arr1.join(";")); //1;true;[object Object];;hello;8;0.8;NaN
console.log(arr1.join("hello"));//1hellotruehello[object Object]hellohellohellohello8hello0.8helloNaN
console.log(arr2.join("你好"));//1你好true你好[object Object]你好你好hello你好8你好0.8你好NaN
// 结果说明null, undefined, 都会转成 '', 即空字符串

// map 使用.
console.log('\nmap() 使用. ');
let rrr = arr1.map( (value,index,arr) => {
    // arr === arr1
    console.log(value,index,arr1 === arr);
    if(value)
        return value;
});

console.log(arr1);
console.log(rrr);
/*
1 0 true
true 1 true
{} 2 true
null 3 true
name 4 true
8 5 true
0.8 6 true
NaN 7 true
undefined 8 true
[ 1, true, {}, null, 'name', 8, 0.8, NaN, undefined ]
[
          1,      true,
         {}, undefined,
     'name',         8,
        0.8, undefined,
  undefined
]
结果说明1  arr === arr1 
结果说明2  map的回调函数可以没有返回值
结果说明3  map函数的返回值是一个新数组, 新数组中的元素值即 回调函数的返回值
结果说明4  map函数的返回值是一个新数组, 这个新数组的长度一定和原数组相同
*/

class AAA{
    constructor(d){
        this.data = d;
    }
    f (value,index,arr){
        // console.log(this.data);// 'hello,world'
        if(value)
            return value;
    }

}

let a = new AAA('hello,world');

// filter 使用.
rrr = arr1.filter(a.f,a);
console.log(arr1);
console.log(rrr);
rrr[2].data = a.data;
console.log(arr1);
console.log(rrr);
// 结果说明: 第二个参数为,this关键字可以在callbackfn函数中引用的对象。
// 结果说明: filter的返回值是一个新数组,元素长度 <= 原数组长度
// 结果说明: filter的返回值是一个新数组,新数组的元素和原数组的元素相等


// sort 排序
let arr3 = [3,1,2,1,7]
rrr = arr3.sort();
console.log(arr3);//[ 1, 1, 2, 3, 7 ]
console.log(rrr);//[ 1, 1, 2, 3, 7 ]
rrr.sort((a,b) => b-a);
console.log(arr3);//[ 7, 3, 2, 1, 1 ]
console.log(rrr);//[ 7, 3, 2, 1, 1 ]

arr3 = [ {name:'zhangsan', age:22},
        {name:'aliba', age:32},
        {name:'lisi', age:32},
        {name:'wangwu', age:22},
        {name:'zhaoliu', age:43},
        { name: 'xuxian', age: 22 }];
// abcdefghijklmnopqrstuvwxyz
// 按照年龄排序从大到小,如果年龄相同,则按照姓名ascii从大到小
arr3.sort((a,b) => {
    if(a.age == b.age){
        // return b.name >= a.name;// 错误写法
        return b.name > a.name?1:-1;
    }else{
        return b.age - a.age;
    }
});
console.log(arr3);
/*
// [按照年龄排序从大到小,如果年龄相同,则按照姓名ascii从大到小] 的结果
[
  { name: 'zhaoliu', age: 43 },
  { name: 'lisi', age: 32 },
  { name: 'zhangsan', age: 22 },
  { name: 'wangwu', age: 22 }
  { name: 'xuxian', age: 22 }
]
// 说明: sort函数默认从小到大排序
// 说明: sort函数返回值 === 原数组
// 说明: sort函数会修改原数组
// 说明: sort函数的参数是一个比较函数cmp_func, 返回值必须是number.[小于0 等于0 大于0].如果cmp_func返回值是一个固定值,则不进行排序
// 说明: sort函数底层类似实现为  if( cmp_func(a,b)>=0 ) swap(a,b);
*/