-1

I have two arrays

self.objarr = [
       { name: ' abc '},
       { name: ' def '},
       { name: ' xyz '}
           ];

self.strarr =[' abc ',' mno '];

I would like to find items in strarr which are not already present in objarr (in the above case 'mno').

UPDATE: Answer links were shown which had arrays of the same object style, so either both were string arrays or if both were objects, they had same style. But in my case, one is an object where the other is a string array. One of the answers was as below:

var x = ["a","b","c","t"];
var y = ["d","a","t","e","g"];

myArray = y.filter( function( el ) {
  return x.indexOf( el ) < 0;
});

I tried something on same lines as below but it did not work:

myArray = self.strarr.filter(function (el) {
                console.log(el);
               return self.objarr.name.indexOf(el) < 0;
            });

I'm reopening with hope that someone can help.

Wtower
  • 18,848
  • 11
  • 103
  • 80
Arnab
  • 2,324
  • 6
  • 36
  • 60
  • http://stackoverflow.com/questions/8628059/check-if-every-element-in-one-array-is-in-a-second-array http://stackoverflow.com/questions/15514907/determining-whether-one-array-contains-the-contents-of-another-array-in-javascri http://stackoverflow.com/questions/16312528/check-if-an-array-contains-any-elements-in-another-array-in-javascript – Eray Balkanli Mar 04 '16 at 13:43
  • @Eray Thanks for the links but could not find answer to my problem in them, maybe I'm missing something.. – Arnab Mar 04 '16 at 13:48
  • @RoyJ Thanks for the link. if you answer from anyone of them,I will accept – Arnab Mar 04 '16 at 13:52
  • @Amab Why would you propose to accept an answer which is a pointer to a duplicate question? Duplicates are closed. –  Mar 04 '16 at 14:04
  • @torazaburo well in that case, I will not accept, I was not aware of that.. – Arnab Mar 04 '16 at 14:27
  • @RoyJ my question is slightly different from the link question as shown in update.. – Arnab Mar 05 '16 at 06:44
  • @Arnab Checkout my answer below, it can filter the data as you require. – Shekhar Chikara Mar 05 '16 at 11:18

1 Answers1

0

Since in your case, you have an object array and another string array, you can use the following code-

myArray = self.strarr.filter(function(strname){
    var element = $.grep(self.objarr, function(el){ return el.name == strname; });
    if(element.length==0) return strname; 
});

Hope this helps!

Shekhar Chikara
  • 3,786
  • 2
  • 29
  • 52