英文:
Trying to match form values with JSON keys, wildcards if no match
问题
这是你提供的JSON结构的翻译:
{
"REGION1": {
"CODE1": {
"mandatory": 1,
"labelId": "labelId",
"disableSearch": 1,
"dropdownId": "dropdownId",
"options": ["opt1", "opt2", "opt3"]
}
},
"*": {
"CODE1": {
"mandatory": 0,
"labelId": "label2Id",
"disableSearch": 1,
"dropdownId": "dropdown2Id",
"options": ["opt1", "opt2", "opt4"]
}
}
}
以下是你提到的关于获取值的翻译:
var values = myJson[regionText][codeText] // 从下拉菜单中获取
if (values[regionText][codeText] == null) {
values = values["*"][codeText];
if (values[regionText][codeText] == null) {
values = values["*"]["*"];
//...
}
}
在这里,我只提供了JSON结构和获取值的相关翻译部分,不包含其他内容。
英文:
Working on a webpage/form I'm trying to use below JSON structure to change/populate the form.
{
"REGION1": {
"CODE1": {
"mandatory": 1,
"labelId": "labelId",
"disableSearch": 1,
"dropdownId": "dropdownId",
"options": ["opt1", "opt2", "opt3"]
}
},
"*": {
"CODE1": {
"mandatory": 0,
"labelId": "label2Id",
"disableSearch": 1,
"dropdownId": "dropdown2Id",
"options": ["opt1", "opt2", "opt4"]
}
}
}
For instance, if a user selects "REGION1" and "CODE1" from some dropdowns, ideally I'd like to grab the values later like:
var values = myJson[regionText][codeText] // grabbed from dropdowns
Now say regionText isn't mapped but codeText is. In theory I want to grab the second object and match the region wildcard. Is there a smart way of:
if(values[regionText][codeText] == null) {
values = values["*"][codeText]
if(values[regionText][codeText] == null) {
values = values["*"]["*"]
//...
}
}
In reality my JSON has 5 layers and if I can avoid 5 layers of if's I'd like to. My current solution is iterating through and matching (not actual JS but pseudocode):
foreach(key in values.keys()) {
if(values[regionText]=="REGION2" or values[regionText]=="*") {
foreach(key2 in values[key].keys()) { // continue }
}
}
EDIT: first answer almost works but fails in this case where I'm searching "REGION1" and "CODE2". In the below case I need to check "REGION1" (doesn't find "CODE2" then check the "*" region for "CODE2" which is there.
{
"REGION1": {
"CODE1": {
"isMandatory": 1,
"labelId": "id",
"disableSearch": 1,
"dropdownId": "id",
"stakeholders": ["opt1", "opt2"]
}
},
"*": {
"CODE2": {
"isMandatory": 1,
"labelId": "id",
"disableSearch": 1,
"dropdownId": "id",
"stakeholders": ["opt1", "opt2"]
}
}
}
答案1
得分: 1
您可以使用这样的简单函数:
function get(values, ...props) {
let current = values;
for (const prop of props) {
if (prop in current) {
current = current[prop];
} else {
current = current['*'];
}
}
return current;
}
编辑:
我已经使用递归函数来使它适用于第二个示例。
const values = {
"REGION1": {
"CODE1": 'REGION1 CODE1',
},
"*": {
"CODE2": '* CODE2',
}
}
console.log(get(values, 'REGION1', 'CODE2'))
function get(values, ...props) {
const rec = (current = values, index = 0) => {
if (index === props.length) return current;
const prop = props[index];
const val = prop in current && rec(current[prop], index + 1);
if (val) return val;
const val2 = '*' in current && rec(current['*'], index + 1);
if (val2) return val2;
return undefined;
}
return rec();
}
不包括代码部分的翻译。
英文:
You can use a simple function like this:
function get(values, ...props) {
let current = values
for (const prop of props) {
if (prop in current) {
current = current[prop]
} else {
current = current['*']
}
}
return current
}
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
const values = {
"REGION1": {
"CODE1": {
"mandatory": 1,
"labelId": "labelId",
"disableSearch": 1,
"dropdownId": "dropdownId",
"options": ["opt1", "opt2", "opt3"]
}
},
"*": {
"CODE1": {
"mandatory": 0,
"labelId": "label2Id",
"disableSearch": 1,
"dropdownId": "dropdown2Id",
"options": ["opt1", "opt2", "opt4"]
}
}
}
console.log(get(values, 'eqw', 'CODE1'))
function get(values, ...props) {
let current = values
for (const prop of props) {
if (prop in current) {
current = current[prop]
} else {
current = current['*']
}
}
return current
}
<!-- end snippet -->
Edit
I've used a recursive function to make it work with the second example
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const values = {
"REGION1": {
"CODE1": 'REGION1 CODE1'
},
"*": {
"CODE2": '* CODE2'
}
}
console.log(get(values, 'REGION1', 'CODE2'))
function get(values, ...props) {
const rec = (current = values, index = 0) => {
if (index === props.length) return current
const prop = props[index]
const val = prop in current && rec(current[prop], index + 1)
if (val) return val
const val2 = '*' in current && rec(current['*'], index + 1)
if (val2) return val2
return undefined
}
return rec()
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论