Best way to search through an array and return result

I have an application where you can add peoples firstname, lastname and phonenumber (eg. "new Member('Thomas', 'Jonsson', '0709454543'"). In the application it's possible to show the members on a web page, but now I also want to be able to search for a member with either firstname, lastname or phone nr.

All the Member objects are stored in an array, and in the code below I'm looping through this array and adds the members firstname, lastname and phonenumber in another array, that i'm looping through.

First of all, by some reason I get the following error message in the console ("Uncaught TypeError: Object 0 has no method 'match"). The second thing is that I haven't got a clue how (if I get this to work and get a match if I'm searching) I can refer the search result to the member in the member array. I hope you understand what I mean and I'm thankful for any help.

Yeah, and if you have any tips in better ways for searching, I'm would of course be very happy.

function searchMember(searchWord){
        var searchArray = [];

        for (var i = 0; i < members.length; i++){
        var firstname = members[i].firstname;
        var lastname = members[i].lastname;
        var phonenr = members[i].phonenr;

        searchArray.push(firstname);
        searchArray.push(lastname);
        searchArray.push(phonenr);
        }

        for (var j = 0; j < searchArray.length; j++) {
            if (searchArray[j].match (searchWord)){
                alert("Found member!");
            }
        }
    }
}

Answers


Your TypeError must be coming from your actual data - is everything really a string?

For the searching - you don't need to do two loops; you can check when iterating the first time already. To get the result, you can return the member in the function. What a function returns is replaced when a function is called.

function searchMember(searchWord) {
    for (var i = 0; i < members.length; i++) {
        var firstname = members[i].firstname;
        var lastname  = members[i].lastname;
        var phonenr   = members[i].phonenr;

        if (firstname.match(searchWord)  // firstname matches
         || lastname.match(searchWord)   // or lastname matches
         || phonenr.match(searchWord)) { // or phonenr matches
            return members[i]; // if either of the three, return this member
        }
    }
}

Then you can do:

var someMember = searchMember("D"); // search for "D"

alert(someMember.phonenr); // alert phonenr of found member

http://jsfiddle.net/Fu7ED/


You don't need to create a search array, you can iterate over the original array as follows:

function searchMember(searchWord) {
    var props = ['firstname', 'lastname', 'phonenr'], j, prop;
    for (var i = 0; i < members.length; i++) {
        for (j = 0; j < props.length; j++) {
            prop = members[i][props[j]];
            if (prop.match && prop.match(searchWord)) {
                alert("Found at index " + i + " and object: " + members[i]);
            }
        }
    }
}

Not necessarily the best solution but you could use

function searchMember(searchWord){
    var str;
    for(i = 0; i< this.length;i++){
        str = this[i].firstname + this[i].secondname + this[i].phonenr;
        if(str.match(searchWord)
            return true;
    }
}

Need Your Help

Parentheses in C, how?

c mathematical-expressions

I want to write this equation in C but I don't know how to use the right parentheses.

Is it good practice to consolidate small static tables in a database?

mysql database django

I am developing a database to store test data. Each piece of data has 11 tags of metadata. Currently I have a separate table for each of the metadata options. I have seen a few questions on here

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.