简体   繁体   中英

Find index of object in an array of prototypes Javascript

I am creating an app using jQuery mobile and need to get the index of an object in an array of prototypes.

The object, call team, is as follows:

var team = function (teamname, colour, rss_url, twitter_url, website, coords) {
    this.teamname = teamname;
    this.colour = colour;
    this.rss_url = rss_url;
    this.twitter_url = twitter_url;
    this.website = website;
    this.location = coords;


};

And the array itself looks like:

var teamlist = [32];

teamlist[0] = new    team("Aberdeen","#C01A1A","http://www.football365.com/aberdeen/rss", "https://twitter.com/aberdeenfc","http://www.afc.co.uk/","57�09?33?N�2�05?20?W");

teamlist[1] = new team("Celtic","#C01A1A","http://www.football365.com/aberdeen/rss", "https://twitter.com/aberdeenfc","http://www.afc.co.uk/","57�09?33?N�2�05?20?W");

teamlist[2] = new team("Dundee","#C01A1A","http://www.football365.com/aberdeen/rss", "https://twitter.com/aberdeenfc","http://www.afc.co.uk/","57�09?33?N�2�05?20?W");

teamlist[3] = new team("Dundee United","#C01A1A","http://www.football365.com/aberdeen/rss", "https://twitter.com/aberdeenfc","http://www.afc.co.uk/","57�09?33?N�2�05?20?W");

teamlist[4] = new team("Hamilton Academical","#C01A1A","http://www.football365.com/aberdeen/rss", "https://twitter.com/aberdeenfc","http://www.afc.co.uk/","57�09?33?N�2�05?20?W");

teamlist[5] = new team("Inverness Caledonian Thistle","#C01A1A","http://www.football365.com/aberdeen/rss", "https://twitter.com/aberdeenfc","http://www.afc.co.uk/","57�09?33?N�2�05?20?W");`

I need to be able to get the index of an object based on teamname. Ihad thought something along the lines of

var a = teamlist.indexOf(teamname: "Aberdeen");

That however, is obviusly not working.

Any suggestions are welcome - thanks in advance! :)

Simple enough. You can use Array.prototype.some , have the index declared as a variable in lexical scope, and change it when a match occurs. Then return the index. Something like this:

 var data = [ {x: '1'}, {x: '2'}, {x: '3'}, {x: '4'} ]; // sample data function findIndex (num) { // num is just the number corresponding to the object // in data array that we have to find var index = -1; // default value, in case no element is found data.some(function (el, i){ if (el.x === num) { index = i; return true; } }); // some will stop iterating the moment we return true return index; } console.log(findIndex('3')); 

Hope that helps!

You can use filter method:

var a = teamlist.filter(function(team) {
    return team.teamname = "Aberdeen";
});

It will produce new array of the teams object with the name of "Aberdeen". If you expect only one team the you need to take the first element from this array: a[0] .

    function getIndex(teamlist, teamname){
        for(var i = 0; i<teamlist.length; i++){
            if(teamlist[i].teamname == teamname){
                return i; // if found index will return
            }
        }
        return -1; // if not found -1 will return
    }

var a = getIndex(teamlist,"Aberdeen"); // will give a as 0

Use this simple index search by value algorithm.

function index_by_value(teamlist, teamname) {
 index = 0

 for (var i = 0; i < teamlist.length; i++) {
   if (teamlist[i].teamname == teamname) {
    index = i;
   }
 }

    return index;

}

index = index_by_value(teamlist, "Aberdeen"); // an example

Keep in mind though that if there are multiple objects in the list with the same teamname, it will return the last one's index. If it does not exist, the function will return 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM