javascript - 当对象深度嵌套在 JavaScript 中时,如何将整个对象作为输出?

标签 javascript object nested

 let common = {
        "id": 1364184,
        "url": "http:\/\/www.tvmaze.com\/episodes\/1364184\/the-big-bang- 
        theory-11x12-the-matrimonial-metric",
        "name": "The Matrimonial Metric",
        "season": 11,
                  "number": 12,
        "airdate": "2018-01-04",
        "airtime": "20:00",
        "airstamp": "2018-01-05T01:00:00+00:00",
        "runtime": 30,
        "image": {
          "medium": "sonie",
          "original": "agarwal" 
        },
        "summary": "<p>To discover who would be most qualified to be best 
         man and maid of honor at their wedding, Sheldon and Amy subject 
         their friends to a series of secret experiments. Also, Penny 
         reveals her true feelings about Amy.<\/p>",
        "_links": {
      "self": {
            "href": "http:\/\/api.tvmaze.com\/episodes\/1364184"
          }
        }
      }
 for (var x in common) {
 console.log(x+ "=" +common[x]);
if ( x === "image" ){
let z = common.image
 for (var y in z) {
  console.log( x + " = " + y+ " : " +z[y]);
 }
 }if ( x === "_links" ){
 let z = common._links.self
 for (var y in z) {
  console.log( x + " = " + y+ " : " +z[y]);
  }
 }
}

output is posted in the image

我写了大部分用于获取输出的代码,但我想要的是,摆脱图中显示的一些用黑色框起来的输出。 所以请帮我...

最佳答案

如果您只是想将深度嵌套的对象作为字符串获取,您可以执行以下操作: JSON.stringify(值)

例如:

var value = {
  one: {
    two: {
      three: [
        'one', 
        'two', 
        'three'
      ]
    }
  }
}
var asString = JSON.stringify(value);

console.log(asString)  # "{"one":{"two":{"three":["one","two","three"]}}}"

关于javascript - 当对象深度嵌套在 JavaScript 中时,如何将整个对象作为输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51911251/

相关文章:

Javascript - 循环内拼接和连接的时间和空间复杂度

javascript - Coffescript/Javascript typeof 的区别

javascript - 如何制作一个按钮来切换两个功能?

javascript - 递归 trim 对象中所有元素的更好方法?

java - 变量、对象和引用之间有什么区别?

java - 如何使用循环用回文填充数组?

javascript - 多个引用的 clearTimeout

Javascript 模板 - 深度嵌套是可能的

javascript - 将嵌套对象的每个对象的索引设置为属性

php - 如何从foreach循环php中的对象数组中删除对象