在原型数组Javascript中查找对象的索引

时间:2015-04-17 13:34:00

标签: javascript jquery arrays mobile prototype

我正在使用jQuery mobile创建一个应用程序,需要在原型数组中获取对象的索引。

对象,呼叫团队如下:

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;


};

阵列本身看起来像:

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");`

我需要能够根据teamname获取对象的索引。

的想法是什么
var a = teamlist.indexOf(teamname: "Aberdeen");
然而,这显然不起作用。

欢迎任何建议 - 提前感谢! :)

4 个答案:

答案 0 :(得分:1)

足够简单。您可以使用Array.prototype.some,将索引声明为词法范围中的变量,并在匹配发生时更改它。然后返回索引。像这样:



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




希望有所帮助!

答案 1 :(得分:0)

您可以使用filter方法:

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

它将生成团队对象的新数组,其名称为" Aberdeen"。如果您只需要一个团队,则需要从该数组中获取第一个元素:a[0]

答案 2 :(得分: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

答案 3 :(得分:0)

按值算法使用此简单索引搜索。

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

请记住,如果列表中有多个具有相同团队名称的对象,它将返回最后一个索引。如果它不存在,该函数将返回0.