JavaScript条件应用于独立对象

3

我正在为我的循环行为感到困惑,希望你能帮忙翻译一下。这是我读到的 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},{...}]}

我创建了两个对象:

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。它将机器人的可用性和性能价值添加到具有相同“region”属性值的机器人的TOWNS对象中。 例如,在我开始时给出的示例中,我会在一个新的对象中找到可用性和性能数据:REGIONS.Ouest {performances:[...], availability:[...]},但是我也会在RENNES的perf和availabilities数组中找到NANTES的perf和availibilities数据…而我不希望出现这种情况!
我的条件/循环有什么问题!?
2个回答

2
您的代码中引用了“town”,但是传入的JSON中却使用了“ville”。 虽然这样做不能解决问题,但至少示例应该是正确的。
传入的JSON有两个子对象。对于每个子对象,您都会在Towns和Regions数据结构中检查其是否存在。如果它们没有匹配的条目,您将创建一个,并在两个进一步的子对象(performances和availability)中添加条目。
如果您不希望在两种情况下都这样做,您需要适当地测试传入的JSON。

你说得对,我忘记翻译了...但这不是问题,认为“ville”是“town” :) - CoStiC
我在第一次回答时纠正了我的示例。 - CoStiC
我说过你是对的,并在第一次回答时更正了我的例子。我从robot.region和robot.town创建每个对象towns和regions。我确实想要这样做。而且,我还想将同一地区所有城镇的值添加到regions对象中。但我不希望即使它们来自同一地区,也将town2的值添加到town1的值中。 - CoStiC

1
这是一个关于变量引用的问题。尝试进行简单更改: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
太棒了 @CoStiC :) - Raja Mohamed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接