首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Javascript中的新对象获取JSON数据键和内部键

如何从Javascript中的新对象获取JSON数据键和内部键
EN

Stack Overflow用户
提问于 2017-10-04 05:40:06
回答 2查看 1K关注 0票数 2

我有一个JSON,因此我需要提取JSON中的所有键。到目前为止,我提取了所有的键,但是我需要从只有键值的多维数组中提取。

代码语言:javascript
复制
{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 300,
      "main": {
        "submain": "Drizzle"
      },
      "description": "light intensity drizzle",
      "icon": "09d"
    }
  ]
}

我需要构造这样的多维数组.

代码语言:javascript
复制
["coord",["lon","lat"],"weather",[["id","main","description",["small_descript","large_description"],"icon"]....] 

我用过的密码是..。

代码语言:javascript
复制
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);

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-04 06:19:36

你可以试试这个片段(它很容易解释)

代码语言:javascript
复制
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));

票数 1
EN

Stack Overflow用户

发布于 2017-10-04 06:31:53

您可以在一些递归读取对象的字段中使用reduce::

代码语言:javascript
复制
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);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46557730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档