0

I have following loop

for (var i in data) {
   console.log(data[i])
}

Data that I for loop is iterating through is

 "data": [
    {
      "MainArea": "North West"
    },
    {
      "MainArea": "South West"
    },

When I run it I get following output in firebug

[Object { MainArea="North West"}, 
Object {  MainArea="South West"}]

What I am trying to get is just the value of it. So that when I tried

console.log(data[i].MainArea)

Hoping to get just the "North West" and South West" values, instead I get "undefined" in my console.

J. Davidson
  • 3,297
  • 13
  • 54
  • 102

3 Answers3

5

Try following:

console.log(data[i].MainArea);

You are trying to get information out of objects property. Since your variables enclosed to json format list you can reach your variables through named properties.

UPDATE: According your addition to the your original question I created DEMO.

var myObj = {"data": [
                        {
                          "MainArea": "North West"
                        },
                        {
                          "MainArea": "South West"
                        },
                    ],
           },
    data = myObj['data'],
    data_length=data.length;

for (var i= 0; i < data_length; i++)
{
   console.log(data[i].MainArea);
}     

Keep in mind that when you are iterating key value pairs you should use for ... in loop while when you are iterating through array you should use for loop. For example if your data property contains sorted array, for ... in loop will not guaranty to loop from array item 0 to N. It means it will not follow sequence. So use only for loops for array and for ... in for non-array objects. Read about this in here. Some jsPerf comparison be seen here.

Community
  • 1
  • 1
Khamidulla
  • 2,927
  • 6
  • 35
  • 59
  • Ok. Can you publish your `data` object initialization? – Khamidulla Jan 14 '14 at 00:32
  • I just updated the code with original data. I am getting this data from stored procedure. Everything is working fine, I just cant figure out how to just get the Value part i.e "North West" ..thanks – J. Davidson Jan 14 '14 at 00:36
  • Here is [Demo](http://jsfiddle.net/Antindexer/4CQNQ/1/). Sorry I was busy and cannot provide answer timely :( – Khamidulla Jan 14 '14 at 00:49
2

The problem is the data object you are iterating has another property called data which is an array containing the target objects, and of course the property key you need to use is MainArea so try

var data = {
    "data": [{
        "MainArea": "North West"
    }, {
        "MainArea": "South West"
    }]
}
for (var i in data.data) {
    console.log(data.data[i].MainArea)
}

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

The object is defined as this:

{ MainArea="North West" }

So it has only one property, called MainArea. There's no property called value so of course value is undefined. Try:

console.log(data[i].MainArea)
David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    That isn't proper JS syntax. Object properties are `key: value`, not `key=value`. – jfriend00 Jan 14 '14 at 00:22
  • @jfriend00: Valid point, but the syntax here seems to be what he's seeing in his debugger. It's just copied/pasted from the question. – David Jan 14 '14 at 00:23