Reputation: 8068
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
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
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
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