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]);
}
}
}
我写了获取输出的大部分代码,但我想要的是,去掉一些带有黑色框的输出,如图所示。所以请帮帮我。
发布于 2018-08-19 02:01:43
如果您只是想将深度嵌套的对象作为字符串获取,则可以执行以下操作:JSON.stringify(value)
举个例子:
var value = {
one: {
two: {
three: [
'one',
'two',
'three'
]
}
}
}
var asString = JSON.stringify(value);
console.log(asString) # "{"one":{"two":{"three":["one","two","three"]}}}"
发布于 2018-08-19 02:08:31
之所以会发生这种情况,是因为每次都会运行以下行,而不进行检查:
console.log(x+ "=" +common[x]);
相反,您可以使用if-else结构:
for (var x in common) {
if (x === "image") {
let z = common.image;
for (var y in z) {
console.log(x + " = " + y + " : " + z[y]);
}
} else if (x === "_links") {
let z = common._links.self;
for (var y in z) {
console.log(x + " = " + y + " : " + z[y]);
}
} else {
console.log(x + "=" + common[x]);
}
}
https://stackoverflow.com/questions/51911251
复制相似问题