简体   繁体   English

JSON / jQuery - 在子对象和父对象之间移动

[英]JSON/jQuery - moving between child and parent objects

I'm currently building a tool for the card game Hearthstone. 我正在为纸牌游戏Hearthstone构建一个工具。 If you're familiar with the game it's basically a tool that allows you to add your in game deck to a list so that you can monitor what cards you have left at all times along with the chance of drawing X card etc. Nothing too fancy but since I am a huge novice to the world of web development I'm using it as an exercise to help me learn more. 如果您熟悉游戏,它基本上是一个工具,允许您将游戏卡片添加到列表中,以便您可以随时监控您剩余的卡片以及绘制X卡等的机会。没有什么太花哨的但是因为我是网络开发世界的一个新手,所以我将它作为一种练习来帮助我学习更多知识。

Anyway on to my problem! 无论如何我的问题!

At the moment I have a JSON database that has every card in hearthstone along with all of the different parameters associated with each card such as name, cost, playerClass etc. 目前我有一个JSON数据库,其中包含了万能卡中的每张卡以及与每张卡相关的所有不同参数,如名称,成本,播放器类等。

I have figured out how to retrieve objects from the database but only via the name, since that's what players will use to search for the card they want to add to their deck.The problem I have at the moment is that the name of the card is a child of the card object which is itself a child of the card set object (basic, classic, naxx, gvg etc) 我已经想出了如何从数据库中检索对象,但只是通过名称,因为这是玩家将用来搜索他们想要添加到他们牌组的卡片。我现在遇到的问题是卡片的名称是卡片对象的子元素,它本身就是卡片组对象的子元素(基本,经典,naxx,gvg等)

I would like to get ALL of the card data back when I search for it by name but try as I might, I can't figure out how to talk to a parent object via it's child. 当我按名称搜索它时,我想获取所有的卡数据,但尽可能地尝试,我无法弄清楚如何通过它的孩子与父对象交谈。

to start with here is the search function from the users input: 从这里开始是用户输入的搜索功能:

  $.getJSON("json/AllSets.json",function(hearthStoneData){ $('.submit').click(function(){ var searchValue = $('#name').val(); var returnValue = getObjects(hearthStoneData, "name", searchValue); console.log(hearthStoneData); console.log(returnValue); }); }); 

and here is the request from the database: 这是来自数据库的请求:

 function getObjects(obj, key, val) { var objects = []; for (var i in obj) { if (!obj.hasOwnProperty(i)) continue; if (typeof obj[i] == 'object') { objects = objects.concat(getObjects(obj[i], key, val)); } else if (i == key && obj[key].toLowerCase() == val.toLowerCase()) { objects.push(obj[i]); } } return objects; } 

And finally here is an example of one of the JSON cards I am trying to talk to. 最后这里是我试图与之交谈的JSON卡之一的示例。

 { "id":"EX1_066","name":"Acidic Swamp Ooze", "type":"Minion", "faction":"Alliance", "rarity":"Common", "cost":2, "attack":3, "health":2, "text":"<b>Battlecry:</b> Destroy your opponent's weapon.", "flavor":"Oozes love Flamenco. Don't ask.", "artist":"Chris Rahn", "collectible":true, "howToGetGold":"Unlocked at Rogue Level 57.", "mechanics":["Battlecry"]} 

So the output im getting when I console log is something like this: 因此,当我控制日志时,输出结果是这样的:

 Object {Basic: Array[210], Classic: Array[387], Credits: Array[17], Curse of Naxxramas: Array[160], Debug: Array[58]…} Basic: Array[210] [0 … 99] 6: Object artist: "Howard Lyon" collectible: true cost: 2 faction: "Neutral" flavor: "This spell is much better than Arcane Implosion." howToGet: "Unlocked at Level 1." howToGetGold: "Unlocked at Level 28." id: "CS2_025" name: "Arcane Explosion" playerClass: "Mage" rarity: "Free" text: "Deal $1 damage to all enemy minions." type: "Spell" 

As you can see, there are several nested arrays before you actually get to the card. 如您所见,在您实际到达卡之前,有几个嵌套数组。 I can sort of visualise in my head what I think needs to be done but I definitely dont feel certain. 我可以在脑海中想象我认为需要做什么,但我绝对不确定。 Also alot of the syntax has been copy/pasted and modified to suit my needs, I'm still a total beginner with this stuff and really have no idea how I would write a fix to this problem myself. 还有很多语法被复制/粘贴和修改以满足我的需要,我仍然是这个东西的初学者,我真的不知道如何自己写这个问题。

Any help is hugely appreciated. 任何帮助都非常感谢。

Thanks 谢谢

I think there's a problem with how the data is stored: 我认为数据的存储方式存在问题:

  1. Every object needs to have a unique id 每个对象都需要具有唯一的ID
  2. Every Child object needs to return a reference to the parentId. 每个Child对象都需要返回对parentId的引用。 This needs to be stored on insert or creation of the child object. 这需要存储在插入或创建子对象上。
  3. There needs to be a way to look up any object by id 需要有一种通过id查找任何对象的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM