尝试将表单数值与JSON键匹配,如果没有匹配,则使用通配符。

huangapple go评论64阅读模式
英文:

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 = {
    &quot;REGION1&quot;: {
        &quot;CODE1&quot;: {
            &quot;mandatory&quot;: 1,
            &quot;labelId&quot;: &quot;labelId&quot;,
            &quot;disableSearch&quot;: 1,
            &quot;dropdownId&quot;: &quot;dropdownId&quot;,
            &quot;options&quot;: [&quot;opt1&quot;, &quot;opt2&quot;, &quot;opt3&quot;]
        }
    },
    &quot;*&quot;: {
        &quot;CODE1&quot;: {
            &quot;mandatory&quot;: 0,
            &quot;labelId&quot;: &quot;label2Id&quot;,
            &quot;disableSearch&quot;: 1,
            &quot;dropdownId&quot;: &quot;dropdown2Id&quot;,
            &quot;options&quot;: [&quot;opt1&quot;, &quot;opt2&quot;, &quot;opt4&quot;]
        }
    }
}

console.log(get(values, &#39;eqw&#39;, &#39;CODE1&#39;))

function get(values, ...props) {
  let current = values
  for (const prop of props) {
    if (prop in current) {
      current = current[prop]
    } else {
      current = current[&#39;*&#39;]
    }
  }
  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 = {
  &quot;REGION1&quot;: {
    &quot;CODE1&quot;: &#39;REGION1 CODE1&#39;
  },
  &quot;*&quot;: {
    &quot;CODE2&quot;: &#39;* CODE2&#39;
  }
}

console.log(get(values, &#39;REGION1&#39;, &#39;CODE2&#39;))

function get(values, ...props) {
  const rec = (current = values, index = 0) =&gt; {
    if (index === props.length) return current
    const prop = props[index]
    const val = prop in current &amp;&amp; rec(current[prop], index + 1)
    if (val) return val
    const val2 = &#39;*&#39; in current &amp;&amp; rec(current[&#39;*&#39;], index + 1)
    if (val2) return val2
    return undefined
  }

  return rec()
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月7日 04:21:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定