1
 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

i wrote maximum of the code for getting output but what i want is, get rid of some output which is boxed with black color which is shown in the figure. so please help me with that...

3
  • 2
    image is not "deeply nested", it's a property of the object. Why can't you just use: let z = common.image without the loop? And what do you mean by "get rid of some output"? Do you want to delete some properties for the object? Commented Aug 18, 2018 at 17:56
  • so baiscally you want all the deep nested keys in the main objects right ? Commented Aug 18, 2018 at 18:01
  • How about ‘console.dir’? Commented Aug 18, 2018 at 18:06

3 Answers 3

1

If you simply want to get the deeply nested object as a string, you can do the following: JSON.stringify(value)

So for example:

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

console.log(asString)  # "{"one":{"two":{"three":["one","two","three"]}}}"
Sign up to request clarification or add additional context in comments.

Comments

0

You could take a recursive approach by keeping the visited keys for nested objects.

Basically it takes an object and gets all key/value pairs from it.

Then it iterate these key/value pairs by using a destructuring assignment in a loop to get separate key and value.

In the loop, value is checked for truthyness, because null is an object, but has no properties, because you need nested objects for a recursive call of getKeyValues.

The type of value is taken and checked against object, then you have an object and take it a object and an array with all keys for the call of getKeyValues.

If not create an output with all keys and the actual value.

function getKeyValues(object, path = []) {
    Object
        .entries(object)
        .forEach(([key, value]) => {
            if (value && typeof value === 'object') {
                return getKeyValues(value, [... path, key])
            }
            console.log([... path, key].join('.'), value);
        });
}

var common = { id: 1364184, url: "http:\\/\\/www.tvmaze.com\\/episodes\\/1364184\\/the-big-bang-theory - 11 x12 - 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" } } };    

getKeyValues(common);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

madam the answer is absoutely cool and its working too but i am unable to understand can you explain me..
0

That is happening because the following line is running every time without checks:

console.log(x+ "=" +common[x]); 

You can instead have an if-else structure:

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]);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.