簡體   English   中英

在 JS 中配對來自嵌套組的 JSON 值

[英]Pairing JSON values from nested groups in JS

我正在學習 JSON 和 JavaScript,到目前為止我能夠閱讀簡單的值。 但是,我不知道如何獲取較低級別的數據對,例如:

{ statusCode: 200,
content: '{"X":{"key":"k","name":"n", "nested":[{"nestedValue":"A","value":"111"},{"nestedValue":"B","value":"222"},{"nestedValue":"3","value":"333","bestValue":false}]}}',
 headers: {.....

我嘗試了簡單的迭代,但這不起作用。 我想要一對,例如 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

我把不同的console.log留給你看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM