2012-01-04 50 views
6

我有以下陣列屬性:檢查如果對象包含具有值

SoftwareBadges = 
[ 
    { Title: "Playtech", Guid: "7e9", xPos: "96" }, 
    { Title: "BetSoft", Guid: "890", xPos: "169" }, 
    { Title: "WagerWorks", Guid: "35c", xPos: "242" }, 
    { Title: "Rival", Guid: "c35", xPos: "314" }, 
    { Title: "NetEnt", Guid: "59e", xPos: "387" }, 
    { Title: "MicroGaming", Guid: "19a", xPos: "460" }, 
    { Title: "Cayetano", Guid: "155", xPos: "533" }, 
    { Title: "OpenBet", Guid: "cfe", xPos: "607" }, 
    { Title: "RTG", Guid: "4e6", xPos: "680" }, 
    { Title: "Cryptologic", Guid: "05d", xPos: "753" }, 
    { Title: "CTXM", Guid: "51d", xPos: "827" }, 
    { Title: "Sheriff", Guid: "63e", xPos: "898" }, 
    { Title: "Vegas Tech", Guid: "a50", xPos: "975" }, 
    { Title: "Top Game", Guid: "0d0", xPos: "1048" }, 
    { Title: "Party Gaming", Guid: "46d", xPos: "1121" } 
] 
現在

,我需要檢查,如果對其中包含的值,並且返回該對象例如:

var test = "7e9" // Guid 

我如何獲得包含此Guid值的對象?樣本中的 ,它應該返回PlayTech對象。

在C#

使用LINQ我可以這樣做: var x = SoftwareBadges.Where(c => c.Guid == test)

我怎麼能做到這一點在JavaScript?

+0

可能重複(http://stackoverflow.com/questions/4432330/in-javascript -given-value-find-name-from-object-literal) – 2012-01-04 13:52:52

+0

@ nikc.org - 它不是重複的,另一個問題是在數組中使用數組,而我在數組中使用json。 – Dementic 2012-01-04 13:58:30

+1

誠然,不是_exact_重複,但肯定你可以申請信息? http://stackoverflow.com/questions/3515934/json-object-value-based-on-other-object-value http://stackoverflow.com/questions/5181493/how-to-find-a-value-in -a-multidimensional-object-array-in-javascript http://stackoverflow.com/questions/1182082/check-for-a-value-in-a-json-object – 2012-01-04 14:04:20

回答

8

由於關聯數組,你可以試試這個:

for (var s in SoftwareBadges) { 
    if (SoftwareBadges[s]["Guid"] == "7e9") 
     alert(SoftwareBadges[s]["Title"]); 
} 
的[在Javascript中,給定值,從對象文本找到名稱]
相關問題