-1

I have two arrays

 var arr1 = [
    {'id':'1','name':'test1','value':'star','role':['monitor']},
    {'id':'2','name':'test2','value':'player','role':['monitor','Supervisor']},
    {'id':'3','name':'test3','value':'saviour','role':['Supervisor']},
    {'id':'4','name':'test4','value':'rock','role':['monitor']},
    {'id':'5','name':'test5','value':'rocky','role':['boxer','monitor']}
]

var arr2 = ['Supervisor','monitor'];

I want to get the result where arr2 values is properly matched with arr1 roles values

Persons having both the category should be pushed to arr3.

So result should be {'id':'2','name':'test2','value':'player','role':['monitor','Supervisor']}.

if arr2 has one value then we can use arr1.indexOf(arr2[0])!='-1' but how to satisfy the "and" condition in the for loop..

I don't want to use this if possible, but it's all I can think of:

if( arr1.indexOf(arr2[0])!='-1' && arr1.indexOf(arr2[1])!='-1'){
    return arr1[i];
}
Chuck Vose
  • 4,560
  • 24
  • 31
Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59
  • 2
    you do an array intersection. if the count of the intersection is the same as the count of your `arr2`, then your arr1 entry contains all of the values in arr2. – Marc B Apr 06 '15 at 18:49
  • Have you considered using a different data structure that could support lookups in a more efficient manner than looping through all records in `arr1`? – Mike Brant Apr 06 '15 at 18:52
  • by different data structure you meant? – Kunal Vashist Apr 06 '15 at 18:58

3 Answers3

1

The main trick here is the equality of the arrays.

// Assuming we have array 'equals' method which compares the array equality
// el['role'] == arr2 won't work
var result = arr1.filter(function(el){
  return el['role'].equals(arr2);
});

So, we can see that we only have to deal with array equality. This post How to compare arrays in JavaScript? has a lot of discussion about it. Someone has even implemented 'equals' method.

Community
  • 1
  • 1
roxxypoxxy
  • 2,973
  • 1
  • 21
  • 28
1

Like @Marc B and @Terry say, you have just to do an intersection.

Here's a version without Jquery :

function intersection(a, b)
{
  var result = [], ai = 0, bi = 0;
  a.sort();
  b.sort();
  while( a.length > ai && b.length > bi )
  {  
     if(a[ai] < b[bi] ){
       ai++; 
     }
     else if(a[ai] > b[bi] ){ 
       bi++; 
     }
     else
     {
       result.push(a[ai]);
       ai++;
       bi++;
     }
  }
  return result;
}

var arr1 = [
    {'id':'1','name':'test1','value':'star','role':['monitor']},
    {'id':'2','name':'test2','value':'player','role':['monitor','Supervisor']},
    {'id':'3','name':'test3','value':'saviour','role':['Supervisor']},
    {'id':'4','name':'test4','value':'rock','role':['monitor']},
    {'id':'5','name':'test5','value':'rocky','role':['boxer','monitor']}
]

var arr2 = ['Supervisor','monitor'];

var arr3 = [];

arr1.forEach(function(value){
  if(intersection(value.role, arr2).length === arr2.length){
    arr3.push(value);  
  }
});

console.log(arr3);
0

You can use a jQuery one-liner to compare array—and the best thing is that they don't have to be in the exact same order. Simply use the .not() method, previously mentioned before.

The only trick is to use $.each() to loop through your first array, and compare the array of the role object against arr2 using .not():

var arr1 = [
    {'id':'1','name':'test1','value':'star','role':['monitor']},
    {'id':'2','name':'test2','value':'player','role':['monitor','Supervisor']},
    {'id':'3','name':'test3','value':'saviour','role':['Supervisor']},
    {'id':'4','name':'test4','value':'rock','role':['monitor']},
    {'id':'5','name':'test5','value':'rocky','role':['boxer','monitor']}
]

var arr2 = ['Supervisor','monitor'];

var arr3 = [];
$.each(arr1, function(idx,person){
    if($(person.role).not(arr2).length === 0 && $(arr2).not(person.role).length === 0) {
        arr3.push(person);
    }
});

console.log(arr3);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Community
  • 1
  • 1
Terry
  • 63,248
  • 15
  • 96
  • 118