Nate
Nate

Reputation: 7866

Text search - filter two words in two different objects/arrays

I'm using angularJS for my webapp and I have the following problem. When I use angularjs' filter to search for, let's say "John Smith", my object, which is built as follow ({firstname: 'John', lastname: 'Smith'}), won't be recognized because the first name and the last name are not part of the same key.

Any idea how to make the space to be either a space or a search in other keys (or arrays)?

Upvotes: 2

Views: 219

Answers (2)

Mosho
Mosho

Reputation: 7078

EDIT:

Per your requirements, I came up with this:

$scope.compFunc = function(actual, expected) {

    var expectedSplit = expected.split(' ');
    var fullActual;
    //Search people for the person the name belongs to
    $scope.names.forEach(function(person){
        angular.forEach(person, function(name){
            if (name === actual) {
                fullActual = person;
            }
        })

    })

    //create an array from the names object for comparison
    var fullActualArray = [];
    angular.forEach(fullActual, function(name, key){
        if (key != '$$hashKey')
            fullActualArray.push(name = name.toLowerCase());  
    })

    fullActualArray.sort();
    expectedSplit.sort();

    return actual.toLowerCase().indexOf(expected.toLowerCase()) > -1|| 
           fullActualArray.join(',') === expectedSplit.join(',');


};

This should work just how you expect.

FIDDLE

Note: I used forEach for the sake of clarity, but if you have large amounts of data you may want to change it to for loops, which are 90% faster and can break when a match is found.


Previous solutions:

It's very simple, checks if either the value entered in the search (expected) exists in either firstname or lastname (actual), or the opposite. So entering "Robert Downy" should work, but so will "Robert blargh" or "abcDownyefg". Checking against the whole thing (firstname + lastname) is something that you have to do outside the comparison function, and is not supported out of the box with angular as far as I know.

I've also used the following to create a new array for comparison using the full names:

$scope.fullnames = $scope.names.map(function(name){
    return {
        firstname: name.firstname,
        lastname: name.lastname,
        fullname :name.firstname + " " + name.lastname
    } 
})

While extending the original array can be done like this:

$scope.names.forEach(function(name){
     angular.extend(name,{fullname:name.firstname + " " + name.lastname});
})

This is simple and effective, and is what I would do.

Upvotes: 1

Yaron Schwimmer
Yaron Schwimmer

Reputation: 5357

You can create your own custom filter, and have it search whatever you want.

Upvotes: 0

Related Questions