2

I need to search an array of objects with an object of search terms and get the index of results in an array.

Let's say I have an array like this:

[
  {
    name: "Mary",
    gender: "female",
    country: "USA",
    orientation: "straight",
    colorChoice: "red",
    shoeSize: 7
  },
  {
    name: "Henry",
    gender: "male",
    country: "USA",
    orientation: "straight",
    colorChoice: "red",
  },
  {
    name: "Bob",
    colorChoice: "yellow",
    shoeSize: 10
  },
  {
    name: "Jenny",
    gender: "female",
    orientation: "gay",
    colorChoice: "red",
  }
]

Now I need to search the array for:

{
  gender: "female"
}

and get result:

[ 0, 3 ]

The search object can be any length:

{
  gender: "female",
  colorChoice: "red"
}

What's the cleanest and most performant way to search the array?

Thanks.

2

2 Answers 2

2

Here's the idea:

function getFemales(myArr){
 var i = myArr.length, ret = [];
 while (i--){
  if ('gender' in myArr[i] && myArr[i].gender === 'female') {
    ret.push(i);
  }
 }
 return ret.sort();
}

see jsfiddle

And more generic:

function findInElements(elArray, label, val){
 var i = elArray.length, ret = [];
 while (i--){
  if (label in elArray[i] && elArray[i][label] === val) {
    ret.push(i);
  }
 }
 return ret.sort();
}

see jsfiddle

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

4 Comments

Any reason you traverse the array in reverse?
Thats pretty damn clever, since the type juggeling makes sure the while stops when i <= 0! You'd have to change your i-- to --i though :)
using --i I would miss element 0. In that case I'd have to use (--i>=0). It's not significantly faster, see jsperf.com/loop-descendening
2

This should do the trick:

function searchArray(fields, arr)
{
    var result = [];            //Store the results

    for(var i in arr)           //Go through every item in the array
    {
        var item = arr[i];
        var matches = true;     //Does this meet our criterium?

        for(var f in fields)    //Match all the requirements
        {
            if(item[f] != fields[f])    //It doesnt match, note it and stop the loop.
            {
                matches = false;
                break;
            }
        }

        if(matches)
            result.push(item);  //Add the item to the result
    }

    return result;
}

For example:

console.log(searchArray({
  gender: "female",
  colorChoice: "red"
},[
  {
    name: "Mary",
    gender: "female",
    country: "USA",
    orientation: "straight",
    colorChoice: "red",
    shoeSize: 7
  },
  {
    name: "Henry",
    gender: "male",
    country: "USA",
    orientation: "straight",
    colorChoice: "red",
  },
  {
    name: "Bob",
    colorChoice: "yellow",
    shoeSize: 10
  },
  {
    name: "Jenny",
    gender: "female",
    orientation: "gay",
    colorChoice: "red",
  }
]));

1 Comment

You can edit the function to return the indexes (i) of the array too, just push that on the results array instead of the item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.