intelis
intelis

Reputation: 8068

JSON loop through single object

How can i get the second values (dates) in a javascript array?

I am new to this and i can't get it to work.

{"0":"11-28-2012","4":"11-29-2012","10":"12-03-2012"} 

Thanks!

Upvotes: 0

Views: 782

Answers (3)

OammieR
OammieR

Reputation: 2850

Not really sure want do you want. But if you want to get date in month-date-year use split().

var jsonDate = {"0":"11-28-2012","4":"11-29-2012","10":"12-03-2012"};

console.log(jsonDate["0"].split('-')[1]); //28
console.log(jsonDate["4"].split('-')[1]); //29
console.log(jsonDate["10"].split('-')[1]); //03

Upvotes: 0

Ping Yin
Ping Yin

Reputation: 31

In javascript, the order of keys is nondeterministic. if you really want, you can use underscore values function

_.values(obj)[1]

Upvotes: 0

timc
timc

Reputation: 2174

A very simple loop is below. You should check that the object hasOwnProperty which is important for more complicated objects.

If your object is called obj:

obj = {"0":"11-28-2012","4":"11-29-2012","10":"12-03-2012"}; 
for (var i in obj) {
    console.log(obj[i]);
}

Or without the loop:

obj = {"0":"11-28-2012","4":"11-29-2012","10":"12-03-2012"};
console.log(obj[0]); // displays "11-28-2012"  

Upvotes: 1

Related Questions