0

Is there a function to find if a array has two or more keys in them? I want the function to return True / False if the target Array has all the keys in result Array.

//Both must be in the array, not in that order tho.
var target = ['love', 'sweden'];

//Object that i want to search.
var result = [{
    title: "Love in Norway!",
    tags: ['love', 'norway', 'birds', 'summer']
}, {
    title: "Love in Sweden!",
    tags: ['love', 'sweden', 'car', 'blue']
}, {
    title: "I am in Norway!",
    tags: ['mytag', 'sweden', 'summer', 'love']
}, {
    title: "Love in randowm!",
    tags: ['love', 'norway', 'birds', 'summer']
}, ];

myfunction(result[x], target, function (isThere) {
    // isThere of [x] = false;
    // isThere of [x] = true;
    // isThere of [x] = true;
    // isThere of [x] = false;
});
Tushar
  • 85,780
  • 21
  • 159
  • 179
Krister Johansson
  • 691
  • 2
  • 10
  • 34
  • What is expected output? – Tushar Nov 25 '15 at 07:14
  • You aren't looking for keys, you are looking for values, the keys of `['love', 'sweden']` are `0 => love` and `1 => sweden`. – SpazzMarticus Nov 25 '15 at 07:16
  • Use `filter`. `function myfunction(result, target) { return result.tags.filter(function (e) { return target.indexOf(e) > -1; }).length === target.length; } var isThere = myfunction(result[2], target);` [**Demo**](https://jsfiddle.net/tusharj/2pe8zyx9/) – Tushar Nov 25 '15 at 07:19

3 Answers3

1

There are a lot of ways to implement it. I have implemented with reduce.

function myFunction(obj, arr) {
   return arr.reduce(function (acc, elem) {
      return acc && obj.tags.indexOf(elem) > -1;
   }, true);
}

console.log(myFunction(result[1], target));

result: true
Isabek Tashiev
  • 1,036
  • 1
  • 8
  • 14
1

As Isa Bek stated, there are a lot of ways:

function isThere(resultSet,target)
{
    for(var i=0;i<target.length;i++)
    {
        if(resultSet.tags.indexOf(target[i])=== -1)
           {
               return false;
           }
    }
       return true;
};


console.log(isThere(result[0],target)); //false
console.log(isThere(result[1],target)); //true
console.log(isThere(result[2],target)); //true
console.log(isThere(result[3],target)); //false

Most of them will use indexOf to check if a value is in an array.

Community
  • 1
  • 1
SpazzMarticus
  • 1,218
  • 1
  • 20
  • 40
1

A case insensitive solution:

var searchWords = ['love', 'sweden'],
    searchArray = [
            {
              title: "Love in Norway!",
              tags: ['love', 'norway', 'birds', 'summer']
            },
            {
              title: "Love in Sweden!",
              tags: ['love', 'sweden', 'car', 'blue']
            },
            {
              title: "I am in Norway!",
              tags: ['mytag', 'SWEDEN', 'summer', 'love']
            },
            {
                title: "Love in randowm!",
                tags: ['love', 'norway', 'birds', 'summer']
            }
    ];

function parser( needle, target ) {
    // Recurring search of needle e.g. first value will be "love"
    needle.forEach( function( searchTag ) {
        // Recurring search of target e.g. first value will be { title: "Love in Norway!", tags: ['love', 'norway', 'birds', 'summer'] }    
        target.forEach( function ( obj ) {
            // See if searchTag exists in tags. If jQuery is on the table then $.inArray() is far more powerfull
            if ( obj.tags.indexOf(searchTag) > -1 ){
                // Your awesome code
                alert ('found "' + searchTag + '" in ' + obj.title );
            }
        } );
    } );
}
parser( searchWords, searchArray );
gmanousaridis
  • 371
  • 5
  • 16