Krister Johansson
Krister Johansson

Reputation: 711

Find if array has two or more keys in them

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;
});

Upvotes: 0

Views: 90

Answers (3)

gmanousaridis
gmanousaridis

Reputation: 373

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 );

Upvotes: 1

SpazzMarticus
SpazzMarticus

Reputation: 1277

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.

Upvotes: 1

Isabek Tashiev
Isabek Tashiev

Reputation: 1044

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

Upvotes: 1

Related Questions