JS.Help

JS.Help

    Array

    
    let fruits = ['apple', 'banana', 'kiwi', 'grape'];
    let nums = [4, 6, 204, 52, 5];
    

    Accessing array elements:

    
    let seasons = ['Winter', 'Spring', 'Summer', 'Autumn'];
    
    seasons[2];  // 'Summer'
    seasons[0];  // 'Winter'
    

    List of methods and techniques which are used to modify an array itself:

    Add new elements:

    push

    The push() method is used to add one or more elements at the end of array.

    let numbers = [4, 22, 13];
    numbers.push(8);  // [4, 22, 13, 8]
    
    

    unshift

    The unshift() method is used to add new elements to the start of the array.

    let phones = [
        { name: 'Samsung',  color: 'white', number: 5 },
        { name: 'Apple',  color: 'black', number: 7 },
        { name: 'Xiaomi',  color: 'gold', number: 4 },
    ]
    
    phones.unshift({ name: 'Huawei',  color: 'blue', number: 6 });  
    
    /*
      [
          { name: 'Huawei',  color: 'blue', number: 6 },
          { name: 'Samsung',  color: 'white', number: 5 },
          { name: 'Apple',  color: 'black', number: 7 },
          { name: 'Xiaomi',  color: 'gold', number: 4 },
      ]
    */
    

    splice

    The splice() method is used to insert (and/or) remove elements in the array.

    let animals = ['tiger', 'monkey', 'giraffe', 'fox', 'panda', 'lion'];
    animals.splice(2, 0, 'elephant');
    console.log(animals);  // expected output: ['tiger', 'monkey', 'elephant', 'giraffe', 'fox', 'panda', 'lion']
    

    Remove elements:

    pop

    The pop() method removes the last element of the array and returns it.

    let cars = ['Honda', 'Toyota', 'Nissan', 'Mercedes', 'Chevrolet', 'BMW'];
    let removed = cars.pop();
    console.log(cars);  // expected output: ['Honda', Toyota, 'Nissan', 'Mercedes', 'Chevrolet']
    console.log(removed);  // expected output: 'BMW'
    

    shift

    The shift method is used to remove first element from the array.

    let colors = ['red', 'orange', 'yellow', 'pink', 'black', 'brown', 'grey'];
    let removed = colors.shift();
    console.log(colors);  // expected output: ['orange', 'yellow', 'pink', 'black', 'brown', 'grey']
    console.log(removed);  // expected output: 'red'
    

    splice

    The splice() method is used to insert (and/or) remove elements in the array.

    let animals = ['tiger', 'monkey', 'giraffe', 'fox', 'panda', 'lion'];
    let removed = animals.splice(2, 3, 'elephant', 'turtle');
    console.log(animals);  // expected output: ['tiger', 'monkey', 'elephant', 'turtle', 'lion'];
    console.log(removed);  //expected output: ['giraffe', 'fox', 'panda']
    

    delete operator

    let fruits = ['apple', 'kiwi', 'banana', 'peach'];
    delete fruits[1];
    console.log(fruits);  // expected output: ['apple', undefined, 'banana', 'peach']
    

    length

    let nums = [5, 2, 72, 19, 15, 0, 23];
    nums.length = 3;
    console.log(nums);  //expected output: [5, 2, 72]
    

    Get the array length:

    let cars = ['Honda', 'Toyota', 'Nissan', 'Mercedes', 'Chevrolet', 'BMW'];
    cars.length;  // 6
    

    Copy an array use slice() method or spread syntax:

    let actors = ['Al Pacino', 'Johnny Depp', 'Tom Hanks', 'Leonardo DiCaprio'];
    let actorsCopy = actors.slice();
    let actorsCopy2 = [...actors];
    

    Merge two or more arrays use concat() method:

    let portion1 = [1, 2, 3, 4];
    let portion2 = [5, 6, 7];
    let nums = portion1.concat(portion2);
    console.log(nums);  // expected output: [1, 2, 3, 4, 5, 6, 7]
    

    Common methods

    • join -
    • reduce -

    MDN: Array

    • Accessing array elements:
    • List of methods and techniques which are used to modify an array itself:
      • Add new elements:
      • Remove elements:
    • Get the array length:
    • Copy an array use slice() method or spread syntax:
    • Merge two or more arrays use concat() method:
    • Common methods