陣列密鑰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);
}
});
});
}
你的代碼是很難看的,請更正縮進。 – borislemke
我糾正了縮進,但語法仍然完全關閉。你能複製並粘貼來自源 –
的實際數據和代碼嗎?這個JSON在世界上所有的國家都是無效的 – quirimmo