[英]Pairing JSON values from nested groups in JS
I am learning JSON and JavaScript and so far I am able to read the simple values.我正在学习 JSON 和 JavaScript,到目前为止我能够阅读简单的值。 However, I do not know how to obtain lower level data pairs, such as in:
但是,我不知道如何获取较低级别的数据对,例如:
{ statusCode: 200,
content: '{"X":{"key":"k","name":"n", "nested":[{"nestedValue":"A","value":"111"},{"nestedValue":"B","value":"222"},{"nestedValue":"3","value":"333","bestValue":false}]}}',
headers: {.....
I tried simple iterating but that does not work.我尝试了简单的迭代,但这不起作用。 I would like to get pairs such as A:111 and B:222.
我想要一对,例如 A:111 和 B:222。
let content = { "X": { "key": "k", "name": "n", "nested": [{ "nestedValue": "A", "value": "111" }, { "nestedValue": "B", "value": "222" }, { "nestedValue": "3", "value": "333", "bestValue": false }] } }; let arr = content.X.nested; Object.entries(arr).forEach((entry) => { console.log(entry[0] + ' - ' + entry[1]); Object.entries(entry[1]).forEach(([key, val]) => { console.log(key + ' / ' + val); }) }) console.log(content.X); //obj console.log(content.X.key); //value console.log(content.X.nested); //array console.log(content.X.nested[0].nestedValue); //value console.log(content.X.nested[1].value); //value
I left the different console.log for you to see我把不同的console.log留给你看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.