簡體   English   中英

如何從嵌套的json數組中提取數據?

[英]How to extract data from nested json array?

每次用戶提交數據時,我都會在嵌套的json數據數組中接收數據,我需要提取該數據。

因此,有時可能是從guestion_1guestion_5 ,然后在另一個示例中是從guestion_1guestion_9 ,依此類推。

通過這種方式,我為每個用戶提交接收動態json數據數組。

可能的json結果示例:

{ 
  question_xx: [ 'Another question?', 'Probably yes' ],
  question_3: [ 'Home origin planet?', 'Mars' ], 
  question_2: [ 'Are you from planet Earth?',   'No' ],  
  question_1: [ 'Home origin Galaxy?', 'Milky Way' ], 
}

我希望輸出為:

家鄉銀河? 銀河
你來自地球嗎? 沒有
本土星球? 火星

等等

您可以使用Object.values將數組作為數組的數組Object.values 您如何去那里取決於您所追求的目標。 為了將輸出作為字符串,可以在外部數組上進行map()並對所有內容進行join()

 let j = { question_xx: [ 'Another question?', 'Probably yes' ], question_3: [ 'Home origin planet?', 'Mars' ], question_2: [ 'Are you from planet Earth?', 'No' ], question_1: [ 'Home origin Galaxy?', 'Milky Way' ], } // array of arrays let arr = Object.values(j) console.log(arr) // join arrays as strings // join inner arrays with space, outer arrays with new line let strings = arr.map(arr => arr.join(" ")).join(' \\n') console.log(strings) 

暫無
暫無

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

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