2015-04-17 51 views
3

我正在使用jQuery mobile創建應用程序,並且需要獲取原型數組中對象的索引。在原型數組中查找對象的索引Javascript

的對象,調用球隊,如下:

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

然而,這顯然是行不通的。

歡迎任何建議 - 在此先感謝! :)

回答

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 

但請記住,如果有在同一teamname列表中的多個對象,它將返回最後一個索引。如果不存在,該函數將返回0.

+0

非常棒 - 運行良好 - 謝謝! :) –

0

您可以使用filter方法:

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

這將產生新的數組的團隊「仔」的對象的名稱。如果您希望只有一個團隊,則需要從該陣列獲取第一個元素:a[0]

+0

只會找到一個團隊(所有團隊名稱都是唯一的)。在你上面的例子中,如何在控制檯中顯示Aberdeen的索引?我將使用索引來引用找到的對象的rss_url。謝謝 –

+0

那麼你只有團隊名稱作爲一個字符串,或者你有團隊實例對象,你想在數組中找到相應的索引? – dfsq

+0

如上圖所示,每個球隊都有一個隊名,顏色,rss_url等等(對不起,如果我不擅長解釋,我需要找到一個特定球隊名稱的索引,例如「Aberdeen」,理想情況下,當我運行函數來查找「Aberdeen」在數組中,我會得到0作爲結果。這樣我就可以使用,例如,teamlist [0] .rss_url。再次感謝 –

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

希望幫助!

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 
+0

非常好!謝謝!! :) –

相關問題