简体   繁体   English

深入搜索JSON值多个级别

[英]Searching JSON value multiple levels deep

I have the following data object, and eventually there will be more similar ones within entities : 我有以下数据对象,最终entities还会有更多类似的entities

function loadCardLibrary() {
    var cardLibrary = {
        entities: [
            {
                name: "Spareparts",
                values: {
                    sickness: 0,
                    attack: 0,
                    scrap: 3,
                    manaCost: 0
                },
                apply: [
                    applyCreature("Mech"),
                    applyHealth(1),
                    applyNoAttack(true)
                ]
            }
        ]
    };
    return cardLibrary;
}

I am trying to write a function (see this example ) to search for a name value, then go in that object and search to check for a specific apply value, like so: 我正在尝试编写一个函数(请参见本示例 )来搜索name值,然后进入该对象并搜索以检查特定的apply值,如下所示:

function checkHasNoAttack(entityName) {
    for (var entity in cardLibrary.entities) {
        if (cardLibrary.entities[entity].name === entityName) {
            // for debugging
            console.log(cardLibrary.entities[entity]);
            console.log(); // blank line
            for (var applied in cardLibrary.entities[entity].apply) {
                if (cardLibrary.entities[entity].apply[applied].applyNoAttack === true) {
                    console.log(entityName + " cannot attack.");
                } else {
                    console.log("test");
                    break;
                }
            }
        }
    }
}

When I call it right after, like this: 当我稍后调用它时,如下所示:

var canItAttack = checkHasNoAttack("Spareparts");

...this is the result: ...这是结果:

{ 
    name: 'Spareparts',
    values: {
        sickness: 0,
        attack: 0,
        scrap: 3,
        manaCost: 0
    },
    apply: [ 'Mech', 1, true ]
}

test

Instead of test which comes from the else , I am trying to log the get it to respond to the first if statement, and log: 而不是test来自elsetest ,我尝试记录get响应第一个if语句,并记录:

Spareparts cannot attack. 备件不能攻击。

With the understanding that more complicated logic will eventually replace the console.log there I really need this to work, but have been tinkering with it and I just can't seem to get it right. 了解到更复杂的逻辑最终将替换console.log ,我确实需要它来工作,但是一直在修改它,而我似乎无法正确地做到这一点。 Any help very appreciated! 任何帮助都非常感谢!

This is pretty simple: 这很简单:

function check(a) {
    var result = null;
    cardLibrary.entities.forEach(function(b){
        if (b.name === a) {
            if (b.apply[2] === true) {
                console.log(a + " cannot attack!");
            } else {
                console.log("test");
            }
            result = b;
        }
    });
    return result;
}

apply is an array, you have to use [2] to select that value apply是一个数组,您必须使用[2]选择该值


Arrays do an ugly job at this, I recommend modifying your apply into an object like: 数组在这方面做得很丑,我建议将您的apply修改为一个对象,例如:

apply: {
    creature: applyCreature("Mech"),
    health: applyHealth(1),
    noAttack: applyNoAttack(true)
}

Then you can use this code: 然后,您可以使用以下代码:

function check(a) {
    var result = null;
    cardLibrary.entities.forEach(function(b){
        if (b.name === a) {
            if (b.apply.noAttack === true) {
                console.log(a + " cannot attack!");
            } else {
                console.log("test");
            }
            result = b;
        }
    });
    return result;
}

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

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