-1
var array1 = [1, 3, 4, 5];
var array2 = [1, 3, 6, 7];

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 = the output

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

Cris
  • 12,799
  • 5
  • 35
  • 50
Monibrata
  • 427
  • 4
  • 10
  • 25
  • 1
    similar question http://stackoverflow.com/questions/17856846/comparing-two-arrays-in-jquery – Cris Mar 04 '14 at 06:14
  • 1
    You just asked an identical question in http://stackoverflow.com/questions/22163143/how-to-find-common-elements-only-between-2-arrays-in-jquery – Amadan Mar 04 '14 at 06:14
  • 1
    The purpose of this site is to help you solve problems you've encountered *while* writing code on your own. Not doing the job for you. – Itay Mar 04 '14 at 06:15
  • Yes I know that. But my output is coming different..,thats why I have placed the question in a different way... I am sorry if I am wasting your time...Thanks !! – Monibrata Mar 04 '14 at 06:25

1 Answers1

0
var array1  = [1, 3, 4, 5],
var array2 = [1, 3, 6, 7];

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

  console.log(common); 



var array3 = array2.filter(function(obj) { return array1.indexOf(obj) == -1; });
monu
  • 370
  • 1
  • 10
  • var array3 = array2.filter(function(obj) { return array1.indexOf(obj) == -1; }); by doing this only 6,7 is coming whereas I want 4,5,6,7 – Monibrata Mar 04 '14 at 06:23
  • @Monibrata- Here is the link according to your requirement http://jsfiddle.net/ygByD/15/ .If you find it useful then accept this answer so that it will help others. – monu Mar 04 '14 at 07:34