如何检索值为 true 的动态 JSON 键?

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

How to retrieve dynamic JSON keys where value is true?

问题

{
"翻译结果": {
"文本": "I have the below JSON response:\n\n{\n "A":{"Option1":true,"Option2":true,"Option3":false}\n "B":{"OptionX":true,"OptionY":true,"OptionZ":false}\n }\n\nI want to get the following values in a string: Option1, Option2, OptionX, OptionY\n\nI have tried the below but with no luck:\n\n Array.from(this.model).forEach(child => {\n console.log(child, 'child name')\n });",
"翻译": "我有以下的JSON响应:\n\n{\n "A":{"Option1":true,"Option2":true,"Option3":false}\n "B":{"OptionX":true,"OptionY":true,"OptionZ":false}\n }\n\n我想要获取以下数值并放在一个字符串中:Option1,Option2,OptionX,OptionY\n\n我尝试了以下方法但没有成功:\n\n Array.from(this.model).forEach(child => {\n console.log(child, 'child name')\n });"
}
}

英文:

I have the below JSON response:

{
  "A":{"Option1":true,"Option2":true,"Option3":false}
  "B":{"OptionX":true,"OptionY":true,"OptionZ":false}
 }

I want to get the following values in a string: Option1, Option2, OptionX, OptionY

I have tried the below but with no luck:

  Array.from(this.model).forEach(child => {
    console.log(child, 'child name')
  });

答案1

得分: 1

const data = {
  "A": {"Option1":true,"Option2":true,"Option3":false},
  "B": {"OptionX":true,"OptionY":true,"OptionZ":false}
};
 
const res = Object.values(data).flatMap(o => Object.keys(o).filter(k => o[k]));

console.log(res)
英文:

Use flatMap() with filter() where you map over each key using Object.keys()

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = {
  &quot;A&quot;: {&quot;Option1&quot;:true,&quot;Option2&quot;:true,&quot;Option3&quot;:false},
  &quot;B&quot;: {&quot;OptionX&quot;:true,&quot;OptionY&quot;:true,&quot;OptionZ&quot;:false}
};
 
const res = Object.values(data).flatMap(o =&gt; Object.keys(o).filter(k =&gt; o[k]));

console.log(res)

<!-- end snippet -->

答案2

得分: 0

for (const record of Object.values(json)) {
  for (const [key, value] of Object.entries(record)) {
    if (value) {
      console.log(key);
    }
  }
}

// or

const keys = Object
  .values(json)
  .map(record => Object
    .entries(record)
    .filter(([key, value]) => value)
    .map(([key]) => key)
  )
英文:
for (const record of Object.values(json)) {
  for (const [key, value] of Object.entries(record)) {
    if (value) {
      console.log(key);
    }
  }
}

// or

const keys = Object
  .values(json)
  .map(record =&gt; Object
    .entries(record)
    .filter(([key, value]) =&gt; value)
    .map(([key]) =&gt; key)
  )

答案3

得分: -1

    const obj = {
      "A": {
        "Option1": true,
        "Option2": true,
        "Option3": false
      },
      "B": {
        "OptionX": true,
        "OptionY": true,
        "OptionZ": false
      }
    }

    const result = Object.values(obj).flatMap(o => Object.entries(o).filter(([key, value]) => value).map(([key]) => key))

    console.log(result)
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const obj = {
  &quot;A&quot;: {
    &quot;Option1&quot;: true,
    &quot;Option2&quot;: true,
    &quot;Option3&quot;: false
  },
  &quot;B&quot;: {
    &quot;OptionX&quot;: true,
    &quot;OptionY&quot;: true,
    &quot;OptionZ&quot;: false
  }
}

const result = Object.values(obj).flatMap(o =&gt; Object.entries(o).filter(([key, value]) =&gt; value).map(([key]) =&gt; key))

console.log(result)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 19:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360610.html
匿名

发表评论

匿名网友

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

确定