JSON:获取映射数据

时间:2016-05-12 16:47:55

标签: javascript json javascript-objects

摘要:

每个dropdowns有三个levelNum。例如,levelNum2的下拉列表包含(Department-Unit-1, Department-Unit-2 & Department-Unit-3),levelNum3包含(Division-Unit-1 & Division-Unit-2),levelNum4包含(Business-Unit-1 & Business-Unit-2)

有一个array of objects。在每个对象中都有一个名为 hierarchyLevels 的属性,它又是array of objects。在每个对象中都有一个属性 unitName & levelNum ,如下面JSON所示。

var data = [{
    "hierarchyLevels": [{
        "unitName": "Department-Unit-3",
        "levelNum": 2
    }, {
        "unitName": "Division-Unit-2",
        "levelNum": 3
    }, {
        "unitName": "Business-Unit-1",
        "levelNum": 4
    }]
}, {
    "hierarchyLevels": [{
        "unitName": "Department-Unit-1",
        "levelNum": 2
    }, {
        "unitName": "Division-Unit-1",
        "levelNum": 3
    }, {
        "unitName": "Business-Unit-2",
        "levelNum": 4
    }]
}, {
    "hierarchyLevels": [{
        "unitName": "Department-Unit-2",
        "levelNum": 2
    }, {
        "unitName": "Business-Unit-1",
        "levelNum": 4
    }]
}]

到目前为止已经尝试过:

function getMultipleObjectFromList(propertyName, value, list){
    return list.filter(function (item) {
        return item.hierarchyLevels.some(function (level) {
            return level[propertyName] === value;
        });
    });
};

var res = getMultipleObjectFromList('unitName','Business-Unit-1',data);

要求:

我希望fetch所有unitName与另一个unitName相关联。因此,如果我从Business-Unit-1下拉列表中选择levelNum4,则其他低级别下拉列表将自动填充与unitName相关联的Business-Unit-1。即levelNum3下拉列表将包含只有Division-Unit-2levelNum2下拉列表才会包含(Department-Unit-2 & Department-Unit-3)

任何直接的帮助都会非常值得注意。谢谢。

1 个答案:

答案 0 :(得分:2)

您的数据结构比您在函数中处理的级别更多。您省略了hierarchyLevels属性,它是一个数组,您不进行迭代。

这是一个提议的适应函数,它返回匹配的hierarchyLevels。我还添加了一个类似的函数,它在匹配时返回内部对象,而不是hierarchyLevels数组。那个可以用来填充你的下拉菜单:



function getMultipleObjectFromList(propertyName, value, list){
    return list.filter(function (item) {
        return item.hierarchyLevels.some(function (level) {
            return level[propertyName] === value;
        });
    });
};

function getDetailFromList(propertyName, value, list){
    return list.map(function (item) {
        return item.hierarchyLevels.filter(function (level) {
            return level[propertyName] === value;
        }).pop(); // only return single match
    }).filter(function (item) { // exclude null
        return item;
    }); 
};


var data = [{
    "hierarchyLevels": [{
        "unitName": "Department-Unit-3",
        "levelNum": 2
    }, {
        "unitName": "Division-Unit-2",
        "levelNum": 3
    }, {
        "unitName": "Business-Unit-1",
        "levelNum": 4
    }]
}, {
    "hierarchyLevels": [{
        "unitName": "Department-Unit-1",
        "levelNum": 2
    }, {
        "unitName": "Division-Unit-1",
        "levelNum": 3
    }, {
        "unitName": "Business-Unit-2",
        "levelNum": 4
    }]
}, {
    "hierarchyLevels": [{
        "unitName": "Department-Unit-2",
        "levelNum": 2
    }, {
        "unitName": "Business-Unit-1",
        "levelNum": 4
    }]
}];

var res = getMultipleObjectFromList('unitName','Business-Unit-1',data);

var level2 = getDetailFromList('levelNum', 2, res);

var level3 = getDetailFromList('levelNum', 3, res);

var output = '<h2>filtered data:</h2>' + JSON.stringify(res, null, 4) + '\n'
           + '<h2>level 2:</h2>' + JSON.stringify(level2, null, 4)  + '\n'
           + '<h2>level 3:</h2>' + JSON.stringify(level3, null, 4);

document.querySelector('pre').innerHTML = output;
&#13;
<pre></pre>
&#13;
&#13;
&#13;