Find index of object in Javascript prototype array

I am building an application using jQuery mobile and you need to get the index of an object in a prototype array.

The object, the call command, looks like this:

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 this:

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 get the index of an object based on the command name. I thought something like lines

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

      

This, however, does not work obviusly.

Any suggestions are appreciated - in advance! :)

+3


source to share


4 answers


Use this simple index value search.

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

      



Be aware that if there are multiple objects in the list with the same command name, it will return the last index. If it doesn't exist, the function will return 0.

0


source


Simple enough. You can use Array.prototype.some

, index declared as a variable in the lexical scope, and change it if it matches. Then return the index. Something like that:



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'));
      

Run codeHide result


Hope it helps!

+1


source


You can use the filter method :

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

      

It will create a new array of command objects named "Aberdeen". If you expect only one team, you need to take the first element of the array: a[0]

.

0


source


    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

      

0


source







All Articles