Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To just merge the arrays (without removing duplicates)</p> <h3>ES5 version use <code>Array.concat</code>:</h3> <pre><code>var array1 = ["Vijendra","Singh"]; var array2 = ["Singh", "Shakya"]; var array3 = array1.concat(array2); // Merges both arrays // [ 'Vijendra', 'Singh', 'Singh', 'Shakya' ] </code></pre> <h3>ES6 version use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment" rel="noreferrer">destructuring</a></h3> <pre><code>const array1 = ["Vijendra","Singh"]; const array2 = ["Singh", "Shakya"]; const array3 = [...array1, ...array2]; </code></pre> <p>Since there is no 'built in' way to remove duplicates (<a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf" rel="noreferrer">ECMA-262</a> actually has <code>Array.forEach</code> which would be great for this), we have to do it manually:</p> <pre><code>Array.prototype.unique = function() { var a = this.concat(); for(var i=0; i&lt;a.length; ++i) { for(var j=i+1; j&lt;a.length; ++j) { if(a[i] === a[j]) a.splice(j--, 1); } } return a; }; </code></pre> <p>Then, to use it:</p> <pre><code>var array1 = ["Vijendra","Singh"]; var array2 = ["Singh", "Shakya"]; // Merges both arrays and gets unique items var array3 = array1.concat(array2).unique(); </code></pre> <p>This will also preserve the order of the arrays (i.e, no sorting needed).</p> <p>Since many people are annoyed about prototype augmentation of <code>Array.prototype</code> and <code>for in</code> loops, here is a less invasive way to use it:</p> <pre><code>function arrayUnique(array) { var a = array.concat(); for(var i=0; i&lt;a.length; ++i) { for(var j=i+1; j&lt;a.length; ++j) { if(a[i] === a[j]) a.splice(j--, 1); } } return a; } var array1 = ["Vijendra","Singh"]; var array2 = ["Singh", "Shakya"]; // Merges both arrays and gets unique items var array3 = arrayUnique(array1.concat(array2)); </code></pre> <p>For those who are fortunate enough to work with browsers where ES5 is available, you can use <code>Object.defineProperty</code> like this:</p> <pre><code>Object.defineProperty(Array.prototype, 'unique', { enumerable: false, configurable: false, writable: false, value: function() { var a = this.concat(); for(var i=0; i&lt;a.length; ++i) { for(var j=i+1; j&lt;a.length; ++j) { if(a[i] === a[j]) a.splice(j--, 1); } } return a; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload