簡體   English   中英

如何從 Javascript 中的新對象獲取 JSON 數據鍵和內部鍵

[英]How to Get JSON data keys and inner keys to from new object in Javascript

我有一個 JSON,從中我需要提取 JSON 中的所有鍵。 到目前為止,我提取了所有鍵,但我需要從只有鍵值的多維數組中提取。

 { "coord": { "lon": -0.13, "lat": 51.51 }, "weather": [ { "id": 300, "main": { "submain": "Drizzle" }, "description": "light intensity drizzle", "icon": "09d" } ] }

我需要構建這樣的多維數組......

 ["coord",["lon","lat"],"weather",[["id","main","description",["small_descript","large_description"],"icon"]....]
我使用的代碼是...

 var keyValue1= []; function keyList(obj) { Object.keys(obj).forEach(function(key) { keyValue1.push(key); if(typeof(obj[key]) == 'object') { keyList(obj[key]); } }); } var obj = {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":{"small_descript":"light intensity drizzle","large_descript":"light intensity drizzle"},"icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200}; keyList(obj); console.log(keyValue1);

你可以試試這個片段(這是不言自明的)

 let getKeyList = obj => Object.keys(obj).reduce((s, key) => { if (Array.isArray(obj[key])) { // if it's an array then reduce all it's values (keys) into an array of arrays... s.push(obj[key].reduce((acc, sub) => { acc.push(getKeyList(sub)); return acc; }, [])) } else { // if it's an object then recurse... if (typeof(obj[key]) === "object") s.push(getKeyList(obj[key])) } // always push the key s.push(key) return s; }, [ /* store */ ]); // the JSON data var x = {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":{"small_descript":"light intensity drizzle","large_descript":"light intensity drizzle"},"icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200}; // then call it as console.log(getKeyList(x));

您可以將 reduce 與一些經常讀取的對象字段一起使用:

 function keyList(obj) { return Object.keys(obj).reduce(function(arr, key) { arr.push(key); if(typeof(obj[key]) === 'object') { arr.push(keyList(obj[key])); } return arr; }, []); } var obj = { "coord": { "lon": -0.13, "lat": 51.51 }, "weather": [ { "id": 300, "main": "Drizzle", "description": { "small_descript": "light intensity drizzle", "large_descript": "light intensity drizzle" }, "icon": "09d" } ], "base": "stations", "main": { "temp": 280.32, "pressure": 1012, "humidity": 81, "temp": { "min": 279.15, "max": 281.15 } }, "visibility": 10000, "wind": { "speed": 4.1, "deg": 80 }, "clouds": { "all": 90 }, "dt": 1485789600, "sys": { "type": 1, "id": 5091, "message": 0.0103, "country": "GB", "sunrise": 1485762037, "sunset": 1485794875 }, "id": 2643743, "name": "London", "cod": 200 } var keyValue1 = keyList(obj); console.log(keyValue1);

暫無
暫無

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

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