【问题标题】:javascript conditions applied on independant objects应用于独立对象的 javascript 条件
【发布时间】:2018-07-20 03:17:24
【问题描述】:

我快疯了,因为我不了解循环的行为! 这是我阅读的 json 示例:

[{"type":"robot","town":"NANTES","region":"Ouest","performances":[{"date":YYYY-MM-DD","value":100},{...}],"availability":[{"date":"YYY-MM-DD","value":100},{...}]},{"type":"robot","town":"RENNES","region":"Ouest","performances":[{"date":YYYY-MM-DD","value":100},{...}],"availability":[{"date":"YYY-MM-DD","value":100},{...}]}

我创建了 2 个对象:

REGIONS = {},TOWNS= {};

这是我收到对象时的功能:

function getRobotsDatas(robotList) {
    for (var i = 0; i < robotList.length; i++) {
        var robot = robotList[i];
// working on TOWNS object
//I check if the "town" object of TOWNS object already exist
        if (TOWNS[robot.town] === undefined) { 
// if not, i create it
            TOWNS[robot.town] = {}; 
//then i push performances datas of my robot in my TOWNS.town object
            TOWNS[robot.town].performances = robot.performances; 
// the same for availability datas
            TOWNS[robot.town].availability= robot.availability; 
        } 

// then I work nearly the same way on REGIONS object.
//i check if the "region" object exist in REGIONS object. If not, i create it and add the perf+availability datas of the robot.
        if (REGIONS[robot.region] === undefined) { 
            REGIONS[robot.region] = {};
            REGIONS[robot.region].performances = robot.performances;
            REGIONS[robot.region].availability= robot.availability;
        } 
// If the REGIONS.region already exist, i just want to add the datas of performances and availability in the already existing arrays of REGIONS.region (performances[] and availabilities[])
        else {
            for (var j = 0; j < robot.performances.length; j++) {
                REGIONS[robot.region].performances.push(robot.performances[j]);
            }
            for (var k = 0; k < robot.availability.length; k++) {
                REGIONS[robot.region].availability.push(robot.availability[k]);
            }
        }
    }

问题在于已经存在的“REGIONS.region”的条件也适用于 TOWNS。它在具有相同“区域”属性值的机器人的 TOWNS 对象中添加性能和可用性值。 例如,在我一开始给出的示例中,我会在一个新对象中找到可用性和性能数据:REGIONS.Ouest {performances:[...], availability:[...]},但我也会在 RENNES 的性能和可用性数组中找到 NANTES 的性能和可用性数据......而且,我不想要!

我的条件/循环有什么问题!???

【问题讨论】:

    标签: javascript loops conditional-statements


    【解决方案1】:

    您的代码引用了“town”,但您传入的 JSON 使用了“ville”。 我明白这不能解决问题,但这个例子至少应该是正确的。

    传入的 JSON 有两个子对象。对于每一项,您都需要测试 Towns 和 Regions 数据结构中的存在性。如果它们没有一个匹配的条目,您将创建一个,然后在另外两个子对象(性能和可用性)中添加条目。

    如果您不希望在这两种情况下都这样做,则需要适当地测试传入的 JSON。

    【讨论】:

    • 你说得对,我忘记翻译了......但这不是问题认为«ville»是«town»:)
    • 我在第一次回答时纠正了我的例子。
    • 我说你是对的,并在我第一次回答时纠正了我的例子。我从robot.region 和robot.town 创建每个对象城镇和地区。我确实想要那个。而且我还想将同一区域中所有城镇的值添加到区域对象中。我不希望将来自 town2 的值添加到 town1,即使它们来自同一地区。
    【解决方案2】:

    这是变量引用的问题。试试这个简单的改变 JSON.parse(JSON.stringify(xxx))

    function getRobotsDatas(robotList) {
        for (var i = 0; i < robotList.length; i++) {
            var robot = robotList[i];
            if (TOWNS[robot.town] === undefined) { 
                TOWNS[robot.town] = {}; 
                TOWNS[robot.town].performances = JSON.parse(JSON.stringify(robot.performances)); 
                TOWNS[robot.town].disponibilite = JSON.parse(JSON.stringify(robot.availability)); 
            } 
            if (REGIONS[robot.region] === undefined) { 
                REGIONS[robot.region] = {};
                REGIONS[robot.region].performances = robot.performances;
                REGIONS[robot.region].availability= robot.availability;
            } 
            else {
                for (var j = 0; j < robot.performances.length; j++) {
                    REGIONS[robot.region].performances.push(robot.performances[j]);
                }
                for (var k = 0; k < robot.availability.length; k++) {
                    REGIONS[robot.region].availability.push(robot.availability[k]);
                }
            }
    
    
       }
    }
    

    JS 很有趣.... :)

    【讨论】:

    • 非常感谢!!!这正是我一直在寻找的,但无法解释:D
    • 伟大的@CoStiC :)
    猜你喜欢
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多