gpt4 book ai didi

javascript - JSON : fetch mapping data

转载 作者:行者123 更新时间:2023-12-02 14:38:59 24 4
gpt4 key购买 nike

摘要:

共有三个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下拉列表,另一个低级下拉列表将自动填充 unitNameBusiness-Unit-1 相关.即levelNum3下拉列表将仅包含 Division-Unit-2levelNum2下拉列表将包含 (Department-Unit-2 & Department-Unit-3) .

最佳答案

您的数据结构的级别比您在函数中处理的级别还要多。您省略了 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;
<pre></pre>

关于javascript - JSON : fetch mapping data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37192955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com