0

I have a json that looks like this:

        "metrics" : {
            "ga:pageviews" : true,
            "bounceRate" : false
        }

which is sub object of a bigger json. I access that object like this:

var metrics = report.specifications[0]

I am trying to iterate through these two properties like this:

    for(var key in metrics) {
        if (metrics.hasOwnProperty(key)) {
            console.log(key + " -> " + metrics[key]);
        }
    }

Although, instead of getting these two properties i get a very big list with other properties of the object.

The first line of the unlimited list i get is:

__parentArray -> { metrics: { 'ga:pageviews': true, bounceRate: false } }

Does this has to do with my issue?

Whole JSON schema looks like this:

reportId: String,
specifications : [{
    metrics: {
        'ga:pageviews': Boolean,
        bounceRate: Boolean
    },
    dimensions: {

    }
}
]
}

Any ideas?

Manos
  • 1,471
  • 1
  • 28
  • 45
  • http://stackoverflow.com/questions/1098040/checking-if-a-key-exists-in-a-javascript-object – Manuel Azar Oct 13 '16 at 15:22
  • No idea. That code should only be iterating two properties, assuming your `metrics` object is what you assume it to be. Not enough info in the question to tell what's wrong. –  Oct 13 '16 at 15:23
  • Yeah its strange i get an unlimited list instead – Manos Oct 13 '16 at 15:24
  • 1
    Unlimited? Or just very long? What are they key names. Do you recognize them at all? Seems like you're just working with some other property or object altogether. –  Oct 13 '16 at 15:25
  • @squint check edit – Manos Oct 13 '16 at 15:26
  • As I can assume you working on 1 level higher than you need. Try: var metrics = report.specifications[0].metrics; – Oleg Imanilov Oct 13 '16 at 15:26
  • @Nosyara I tried i get a different long list – Manos Oct 13 '16 at 15:28
  • @Nosyara The last lines look like this now bounceRate -> false ga:pageviews -> true id -> undefined _id -> undefined dimensions -> undefined metrics -> undefined – Manos Oct 13 '16 at 15:30
  • I'm guessing you're just logging an entire object somewhere. Probably getting a lot of properties with leading underscores, right? I think NodeJS may show inherited properties when logging objects. Is this actually affecting your code? –  Oct 13 '16 at 15:30
  • @squint I dont think so the only logging i do is that one – Manos Oct 13 '16 at 15:32

2 Answers2

0

Solved by adding the toObject() method here:

    var metrics = report.specifications[0].metrics.toObject();
Manos
  • 1,471
  • 1
  • 28
  • 45
0

The best way to iterate through properties in JavaScript is using Object.keys. The difference between doing this and using a for loop is that Object.keys ignores properties in the prototype chain, which you are probably picking up.

Object.keys(metrics).forEach(function (key) {
    console.log(key + " -> " + metrics[key]);
});
Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
  • Incorrect answer previously -- pardon me; that was a lazy copy-paste attempt. It is useful to know about the difference between a for loop and Object.keys, though. – Rob Lyndon Oct 13 '16 at 16:52