12
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];

I have two arrays like above. Now I want to do the following in MVC 4 with jQuery.

  1. If every elements of both arrays are equal then show a message/alert. e.g. "All records already existing."

  2. If every elements of both the arrays are different then just add them all in a "VAR", e.g. var resultset = .... (where 7,8,9 will stored)

  3. If few elements common between two arrays then for the common elements show a message with element, e.g. "Record 1,2,3,4,5,6 are already exists" and add the different elements in "VAR", e.g. var resultset = .... (where 7,8,9 will stored). Both the message and difference elements collection will perform at the same time.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Monibrata
  • 427
  • 4
  • 10
  • 25

3 Answers3

34

Try this:

    var array1  = [1, 2, 3, 4, 5, 6],
    array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var common = $.grep(array1, function(element) {
    return $.inArray(element, array2 ) !== -1;
});

console.log(common); // returns [1, 2, 3, 4, 5, 6];



var array3 = array2.filter(function(obj) { return array1.indexOf(obj) == -1; });

// returns [7,8,9];
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
6

Here is my version

function diff(arr1, arr2) {
        var obj = {}, matched = [],
            unmatched = [];
        for (var i = 0, l = arr1.length; i < l; i++) {
            obj[arr1[i]] = (obj[arr1[i]] || 0) + 1;
        }
        for (i = 0; i < arr2.length; i++) {
            var val = arr2[i];
            if (val in obj) {
                matched.push(val);
            } else {
                unmatched.push(val);
            }
        }
        // Here you can find how many times an element is repeating.
        console.log(obj);
        // Here you can find what are matching.
        console.log(matched);
        // Here you can check whether they are equal or not.
        console.log('Both are equal ? :' + 
        matched.length === a.length);
        // Here you can find what are different  
        console.log(unmatched);
    }
redV
  • 684
  • 1
  • 9
  • 26
  • Lovely. This is the only function I could find that gives me a matched `[2]` when comparing `[2, 2, 2]` with `[2, 5, 5]`. Thanks. – Avatar Oct 11 '17 at 09:00
  • PS: Does not work for `diff([2, 2, 2, 5], [2, 2, 5, 5])` which will result in matches `[2, 2, 5, 5]` instead of expected `[2, 2, 5]`. http://jsfiddle.net/hrLHR/14/ – Avatar Oct 11 '17 at 10:41
5

If you do this kind of thing regularly, you may be interested in a Set object that makes this kind of stuff pretty easy:

var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var common = new Set(array1).intersection(array2).keys();

The open source Set object (one simple source file) is here: https://github.com/jfriend00/Javascript-Set/blob/master/set.js

Along with the intersection() method used here, it has all sorts of other set operations (union, difference, subset, superset, add, remove ...).

Working demo: http://jsfiddle.net/jfriend00/5SCdD/

jfriend00
  • 683,504
  • 96
  • 985
  • 979