JSON : 获取映射数据
JSON : fetch mapping data
总结:
每个levelNum
对应三个dropdowns
。例如 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
关联。因此,如果 select Business-Unit-1
来自 levelNum4
下拉列表,另一个低级别下拉列表将自动填充与 Business-Unit-1
关联的 unitName
。即 levelNum3
下拉列表将仅包含 Division-Unit-2
,而 levelNum2
下拉列表将包含 (Department-Unit-2 & Department-Unit-3)
.
任何即时帮助都将不胜感激。谢谢。
你的数据结构比你在函数中处理的层次更多。有你省略的hierarchyLevels
属性,它是一个数组,你不迭代。
这是一个提议的改编函数,returns 那些 hierarchyLevels
匹配。我还添加了一个类似的功能,当有匹配项时 returns 内部对象,而不是 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>
总结:
每个levelNum
对应三个dropdowns
。例如 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
关联。因此,如果 select Business-Unit-1
来自 levelNum4
下拉列表,另一个低级别下拉列表将自动填充与 Business-Unit-1
关联的 unitName
。即 levelNum3
下拉列表将仅包含 Division-Unit-2
,而 levelNum2
下拉列表将包含 (Department-Unit-2 & Department-Unit-3)
.
任何即时帮助都将不胜感激。谢谢。
你的数据结构比你在函数中处理的层次更多。有你省略的hierarchyLevels
属性,它是一个数组,你不迭代。
这是一个提议的改编函数,returns 那些 hierarchyLevels
匹配。我还添加了一个类似的功能,当有匹配项时 returns 内部对象,而不是 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>