skip to Main Content

take the two variables a and b as a two different arrays the variable c shows the output in only one array that means this method merges the two or more arrays into a single array

let a=[a,b];
let b=[c,d];
c=[a,...b]

output = [[a,b],c,d]

2

Answers


  1. If you want merge array a & b you should use spread like this:
    let c=[...a,...b]

    Login or Signup to reply.
  2. You can spread both arrays in order to get merged one single dimentional array

    let a=["a","b"];
    let b=["c","d"];
    c=[...a,...b]
    

    output

    c = ["a", "b", "c","d"];
    

    Spread operator in javascript spreads contents of Array and Objects,
    So, writing

    const a = [1,2,3,4];
    ...a // means 1,2,3,4 (without wrapper array)
    

    you can use these spreaded elements,

    In a Function as arguments

    function sum(a,b,c,d){
     return a+b+c+d;
    }
    const nums = [1,2,3,4];
    const result = sum(...nums); // becomes ->  sum(1,2,3,4);
    

    To Form Another Array

    const nums = [1,2,3,4];
    const nums2 = [...nums, 5,6,7,8]; // -> [1,2,3,4,5,6,7,8];
    

    You can also create object

    const nums = [1,2,3,4];
    const obj = {...nums};
    
    /*
    Output
    
    {
      "1":1,
      "2":2,
      "3":3,
      "4":4
    }
    
    */
    
    

    And Many more things

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search