天天看點

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);
*/