js配列方法

4399 ワード

//      
    var fruits = ['  ', '  ','  ','  ','  '];


    //          ,     
    var newLength = fruits.push('  ');
    //console.log(newLength) //6
    //console.log(fruits) //["  ", "  ", "  ", "  ", "  ", "  "]



    //         ,     
    var last = fruits.pop()
    //console.log(last)//  
    //console.log(fruits) //["  ", "  ", "  ", "  ", "  "]



    //          ,     
    var first = fruits.shift(); 
    //console.log(fruits) //["  ", "  ", "  ", "  "]



    //          ,     
    var newLength = fruits.unshift('  ') 
    //console.log(fruits) // ["  ", "  ", "  ", "  ", "  "]



    //reverse()              ,      。               ,              。
    //     
    console.log(fruits.reverse()) //["  ", "  ", "  ", "  ", "  "]



    //             
    var pos = fruits.indexOf('  ');
    //console.log(pos) //-1         



    //                              ,     
    var removedItem = fruits.splice(1, 1)
    //console.log(fruits) //["  ", "  ", "  ", "  "]



    //Array.from()                      ,        。
    //          
    console.log(Array.from('foo')); // ["f", "o", "o"]



    //Array.isArray()               Array。
    console.log(Array.isArray([1, 2, 3]));  //true
 //concat()              。           ,         。
    const array1 = ['a', 'b', 'c'];
    const array2 = ['d', 'e', 'f'];
    const array3 = array1.concat(array2);

    console.log(array3); //["a", "b", "c", "d", "e", "f"]



    //join()        (        )                     。
    //   ("")          
    const elements = [' ', ' ', ' '];
    console.log(elements.join()); //" , , "
    console.log(elements.join('')); //"   "
    console.log(elements.join('-'));//" - - "



    //slice()              
    //        ,            
    //     ,      ,     ,       ,     
    //         index  ,     (      )      
    const animals = ['1', '2', '3', '4', '5'];
    console.log(animals.slice(2));  //["3", "4", "5"]
    console.log(animals.slice(2, 4)); // ["3", "4"]
    console.log(animals.slice(1, 5)); // ["2", "3", "4", "5"]



    //toString()        
    const array4 = [1, 2, 'a', '1a'];
    console.log(array4.toString()); //"1,2,a,1a"



    //includes                    ,      true,  false。
    let site = ['runoob', 'google', 'taobao'];
    site.includes('runoob'); //true
 var fruits = ['  ', '  ','  ','  ','  '];
    //  forEach                 
    fruits.forEach(function (item, index) {
        // console.log(item, index);
        //    0
        //    1
        //    2
        //    3
        //    4
    });



    //every()                              。        。
    const isBelowThreshold =function(currentValue){
        return currentValue < 40 
    };
    const array1 = [1, 30, 39, 29, 10, 13];
    console.log(array1.every(isBelowThreshold)); //true



    //some()              1              。
    //       Boolean    
    //            ,            false。
    const array = [1, 2, 3, 4, 5];
    const even = (element) => element % 2 === 0;
    console.log(array.some(even)); //true



    //filter()          ,                     。
    const words = ['       ', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    const result = words.filter(word => word.length > 6);
    console.log(result); //["       ", "exuberant", "destruction", "present"]



    //map()          ,                             。
    var numbers = [2, 4, 6];
    var doubles = numbers.map(function(num,index) {
    console.log(index) //0,1,2
    return num * 2;
    });
    console.log(doubles); //[4, 8, 12]