0

So, title says most of it, and I'll give an example:

var foo = ['one', 'two', 'three'];
var bar = [
        {   
            integer: 1,
            string: 'one'
        },
        {   
            integer: 3,
            string: 'three'
        }       
    ];

Now, I'm wondering how can I get the list of positive matches for each of the elements in foo against each of the string properties of all the objects in array bar.

4 Answers 4

4

First, make a set of all elements in foo to avoid square complexity:

var fooSet = {};    
for(var i = 0; i < foo.length; i++) {
    var e = foo[i];
    fooSet[e] = true;
}

Then, go through bar and gather matches:

var matches = [];
for(var i = 0; i < bar.length; i++) {
    var e = bar[i];
    if(fooSet[e.string]) {
           matches.push(e);
    }
}

After this, matches array will contain the elements from bar that match the elements of foo.

Here's a live example:

Sign up to request clarification or add additional context in comments.

3 Comments

+1. Creating fooSet makes this better than my answer that used .indexOf() (especially now that you've removed the for..in loops).
True, but this works only if foo and bar lengths are equal, which is not in my usecase. That's why I gave such an example. jsfiddle.net/mfAc4
It works fine for different lengths of foo and bar - here's an example where foo has 8 and bar has 3 elements: jsfiddle.net/mfAc4/1 and here's an example where foo has 2 and bar has 3 elements: jsfiddle.net/mfAc4/2. Is this what you are looking for?
0

Loop through one of the arrays checking each item against the other:

var matches = [],
    i, j;

for (i=0; i < bar.length; i++)
    if (-1 != (j = foo.indexOf(bar[i].string)))
       matches.push(foo[j]);

// matches is ['one', 'three']

Obviously if you want matches to contain the items from bar rather than foo just change the .push() statement to matches.push(bar[i]);.

Of course the above assumes you don't care about supporting older browsers that don't do .indexOf() on arrays, or that you can use a shim.

Comments

0

These 2 work. Depends on how you want to use the data you can modify.

http://jsfiddle.net/KxgQW/5/

WAY ONE

//wasn't sure which one you want to utilize so I'm doing both
//storing the index of foo to refer to the array
var wayOneArrayFOO= Array();
//storing the index of bar to refer to the array
var wayOneArrayBAR= Array();

var fooLength = foo.length;
var barLength = bar.length;


/*THE MAGIC*/                
//loop through foo
for(i=0;i<fooLength;i++){

    //while your looping through foo loop through bar        
    for(j=0;j<barLength;j++){
        if(foo[i]==bar[j].string){
            wayOneArrayFOO.push(i);
            wayOneArrayBAR.push(j);    
        }
    }

}

WAY TWO

//storing the value of foo
var wayTwoArrayFOO= Array();
//storing the value of bar
var wayTwoArrayBAR= Array();            


/*THE MAGIC*/      
//loop through foo
for(i=0;i<fooLength;i++){

    //while your looping through foo loop through bar        
    for(j=0;j<barLength;j++){
        if(foo[i]==bar[j].string){
            wayTwoArrayFOO.push(foo[i]);
            wayTwoArrayBAR.push(bar[j]);    
        }
    }

}

Comments

0

This:

// foo & bar defined here

// put all the keys from the foo array to match on in a dictionary (object)
var matchSet = {};
for (var item in foo)
{
    var val = foo[item];
    matchSet[val] = 1;
}

// loop through the items in the bar array and see if the string prop is in the matchSet
// if so, add it to the matches array
var matches = [];
for (var i=0; i < bar.length; i++)
{
    var item = bar[i];
    if (matchSet.hasOwnProperty(item['string']))
    {
        matches.push(item['string']);
    }
}

// output the matches (using node to output to console)
for (var i in matches)
{
    console.log(matches[i]);
}

Outputs:

match: one
match: three

NOTE: using node to run interactively

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.