2017-05-26 251 views
-2

陣列密鑰i有一個對象,它是一個解析的JSON的結果:查找有匹配元素ID

{ 
"AKH":{ 
    "name": "Amonkhet", 
    "code": "AKH", 
    "releaseDate": "2017-04-28", 
    "border": "black", 
    "block": "Amonkhet", 
    "type": "expansion" 
    "cards": [ 
    { 
    "artist": "Min Yum", 
    "cmc": 5, 
    "colorIdentity": [ 
    "W" 
    ], 
    "colors": [ 
    "White" 
    ], 
    "manaCost": "{3}{W}{W}", 
    "multiverseid": 426703, 
    "name": "Angel of Sanctions", 
    "number": "1", 
    "power": "3", 
    "rarity": "Mythic Rare", 
    "subtypes": [ 
    "Angel" 
    ], 
    "toughness": "4", 
    "type": "Creature — Angel", 
    "types": [ 
    "Creature" 
    ] 
}, 
    "SHM" : { 
    "name": "Shadowmoor", 
    "code": "SHM", 
    "magicCardsInfoCode": "shm", 
    "releaseDate": "2008-05-02", 
    "border": "black", 
    "type": "expansion", 
    "block": "Shadowmoor", 
    "cards": [ 
{ 
    "artist": "Kev Walker", 
    "colors": [ 
    "White" 
    ], 
    "manaCost": "{W}", 
    "name": "Apothecary Initiate", 
    "number": "1", 
    "power": "1", 
    "rarity": "Common", 
    "subtypes": [ 
    "Kithkin", 
    "Cleric" 
    ], 
    "toughness": "1", 
    "type": "Creature — Kithkin Cleric", 
    "types": [ 
    "Creature" 
    ] 
}, 

通知該鍵的代碼相匹配...這將始終是這種情況下的情況。

讓我從名字元素和代碼

setList += "<a href='#' onClick='setLinks(this.id)' id=" + setCode + ">" + setName + "</a> " + "<br>" 

,我現在試圖創建一個與我的對象密鑰的ID(setCode)相匹配的功能。

現在我有一個確實發現該卡名稱,並列出他們所有的功能:

function cardPop(listings){ 
    var cards = listings.cards; 
    for (var i=0; i < cards.length; i++){ 
    var card = cards[i]; 
    cardNum = Object.keys(cards) 
    } 

    $.each(cards, function(cardNum, cardListing) { 
    cardName = cardListing.name 
    console.log(cardName) 
    }); 
}; 

但在現實中,我想該卡的人口只給卡相匹配的列表點擊鏈接。

function setLinks(clicked_id) 
    if clicked_id == ? 

我用一堆東西打了個哆嗦。我已經衝浪了stackoverflow和全能谷歌,但我很難用我的問題來獲得正確的結果。有些東西確實讓我沒有雪茄。任何幫助是極大的讚賞!

編輯:

@gvmani ..非常感謝您的幫助!這是我最終的結果。不是最終產品,而是現在工作!

$(document).ready(function(){ 
    $.getJSON('https://api.myjson.com/bins/s7and', function (data) { 
    json = data 
    set_codes = Object.keys(data); 
    for (var i=0; i < set_codes.length; i++){ 
    set = set_codes[i]; 
} 
$.each(data, function(set_codes, listings){ 
    setName = listings.name 
    setCode = listings.code 
    setPop(setCode, setName) 
}); 
}); 
}); 

//Set population 
function setPop(){ 
    var setList = ""; 
    setList += "<a href='#' onClick='setLinks(this.id)' id=" + setCode + ">" + setName + "</a> " + "<br>" 
$('.things').append(setList); 
}; 

// Function to Generate Card List 
function setLinks(clicked_id){ 
    $.each(json, function(setCode, setListing) { 
    $.each(setListing.cards,function(cardNum, cardListing){ 
    if(setListing.code === clicked_id){ 
    console.log(cardListing.name); 
} 
}); 
}); 

} 
+0

你的代碼是很難看的,請更正縮進。 – borislemke

+0

我糾正了縮進,但語法仍然完全關閉。你能複製並粘貼來自源 –

+2

的實際數據和代碼嗎?這個JSON在世界上所有的國家都是無效的 – quirimmo

回答

0

根據評論發佈更新。

var data = { 
 
    \t "AKH": { 
 
    \t \t "name": "Amonkhet", 
 
    \t \t "code": "AKH", 
 
    \t \t "cards": [{ 
 
    \t \t \t "name": "1" 
 
    \t \t }, { 
 
    \t \t \t "name": "2" 
 
    \t \t }] 
 
    \t }, 
 
    \t "SHM": { 
 
    \t \t "name": "Shadowmoor", 
 
    \t \t "code": "SHM", 
 
    \t \t "cards": [{ 
 
    \t \t \t "name": "3" 
 
    \t \t }, { 
 
    \t \t \t "name": "4" 
 
    \t \t }] 
 
    \t } 
 
} 
 
    
 
$(document).ready(function(){ 
 
    
 
$.each(data, function(cardNum, cardListing) { 
 
     
 
    $("<a href='#' onClick='setLinks(\""+cardNum+"\")' id=" + cardListing.code + ">" + cardListing.name + "</a> <br>").appendTo("#links"); 
 
    
 
    }); 
 

 

 

 
}); 
 
    
 
function setLinks(code){ 
 
$("#cards").empty(); 
 
    $.each(data, function(cardNum, cardListing) { 
 
    $.each(cardListing.cards,function(ind,card){ 
 
    if(cardListing.code === code){ 
 
    
 
    $("<a href='#'>" + card.name + "</a> <br>").appendTo("#cards"); 
 
    } 
 
    }); 
 
    }); 
 

 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 
<div id="links"> 
 

 
</div> 
 
<div id="cards"> 
 
</div> 
 
</body>

+0

這是倒退。雖然我可能能夠扭轉它,所以我很感激它。 我在尋找的是製作一個列表不是卡片,而是集合代碼,然後單擊代碼集,然後填充該集合中的卡片列表。 –

+0

@ A.MuSanKitty發佈更新 – gvmani

+0

謝謝!我更新了我提出的問題!我很欣賞巨人在正確的方向推進:) –