Array merge in JavaScript

What makes array to merge in JavaScript?

Actually there aren’t so many array functions in JavaScript. As you know you can join, sort, etc. which methods are pretty basic. What I’d like to do is something similar to PHPs’ array_merge and than array_unique.

With JavaScript that seems pretty impossible. You can merge them with the .concat function as in the example bellow:

var a = [1,2];
var b = [3,4];
a.concat(b);

this gives you array with four elements. But when it comes to trim all the duplicates as it’s array_unique in PHP, well that’s really impossible. You’ve to do it by yourself, and I don’t thing this is the best technique. That slows down the code and you should avoid it!

Related posts:

  1. Friday Algorithms: JavaScript Merge Sort
  2. JavaScript array.pop() – Get The Last Item
  3. Array hint in JavaScript
This entry was posted in javascript, micro tutorial and tagged , , , , , , , . Bookmark the permalink.

One Response to Array merge in JavaScript

  1. James Heo says:

    var a = [1,2];
    var b = [3,4];
    var c=(a+’,'+b).split(‘,’);

    c equal to a.concat(b)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">