英文:
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 = {
"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)
<!-- 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 => Object
.entries(record)
.filter(([key, value]) => value)
.map(([key]) => 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 = {
"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)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论