foreach for javascript,json array

I have a JSON array that looks like this:

[{"RegDate":"31-03-2011"},{"RegDate":"29-07-2011"},{"RegDate":"09-08-2011"},{"RegDate":"09-08-2011"},{"RegDate":"09-08-2011"},{"RegDate":"12-08-2011"},{"RegDate":"15-08-2011"},{"RegDate":"15-08-2011"},{"RegDate":"23-08-2011"},{"RegDate":"07-09-2011"},{"RegDate":"09-09-2011"},{"RegDate":"13-10-2011"},{"RegDate":"13-10-2011"},{"RegDate":"13-10-2011"},{"RegDate":"25-10-2011"},{"RegDate":"25-10-2011"},{"RegDate":"03-11-2011"},{"RegDate":"03-11-2011"},{"RegDate":"11-11-2011"},{"RegDate":"16-11-2011"},{"RegDate":"18-11-2011"},{"RegDate":"21-11-2011"},{"RegDate":"02-12-2011"},{"RegDate":"02-12-2011"},{"RegDate":"12-12-2011"}]

The code to get this json array is as of the following:

var unavailableDates1 = jQuery.parseJSON('<?php echo json_encode($noticesDates) ?>');

I am trying too get all of the dates in that array (which was originally a multidimensional array), and put it inside one array:

var unavailableDates = ["9-3-2012", "14-3-2012", "15-3-2012"]; for example

I am unsure of how to do this, I have tried a foreach but wasn't successful.

All help will be appreciated.

var justDates = [];
for (var i=0; i < unavailableDates1.length; i++) {
   justDates.push(unavailableDates1[i]['RegDate']);
}

Each Object ({'RegDate:'date'}) is an item in a javascript array. To get an item you use a numerical index. so to get the first item would be theArray[0] this would give you {"RegDate":"31-03-2011"}. Then to get that particular date string you just have to use the key theArray[0]['RegDate']!. So since you want to go through every member of a list, you should get the list length using .length.

For loops are usually used for this type of iteration and not for..in. because for in can access properties that are undesired! http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/

First of all, that parseJSON is unnecessary and actually dangerous. Take it out:

var unavailableDates1 = <?php echo json_encode($noticesDates) ?>;

Then, just use jQuery.map:

var unavailableDates = $.map(unavailableDates1, function(item) {
    return item.RegDate;
});

Using a string like that is dangerous for the following three objects, for example:

  • {"key":"Backslash here: \\"}
  • {"key":"I'm a horse!"}
  • {"key":"Some\\backslash"}

They become, respectively:

  • SyntaxError: Unexpected end of input (Single escape escapes end of string)
  • SyntaxError: Unexpected identifier (Unescaped single quote breaks out of the string, actually an XSS vulnerability)
  • Object
    key: "Someackslash" (\\b becomes the \b backspace control character)
    __proto__: Object

You could escape it again, but there's no need; valid JSON is always a valid JavaScript object, and you can trust your own PHP not to include injection code in there.

for(var i=0;i<unavailableDates.length;i++) {
    unavailableDates[i] = unavailableDates[i].RegDate;
}

have a look at how to iterate over an array. You can do something like:

dates = new Array();
unavailableDates1.forEach(function (obj){ 
    dates.push(obj.RegDate);
};

Try the following:

JavaScript:

var x = [{"RegDate":"31-03-2011"},{"RegDate":"29-07-2011"},{"RegDate":"09-08-2011"},{"RegDate":"09-08-2011"},{"RegDate":"09-08-2011"},{"RegDate":"12-08-2011"},{"RegDate":"15-08-2011"},{"RegDate":"15-08-2011"},{"RegDate":"23-08-2011"},{"RegDate":"07-09-2011"},{"RegDate":"09-09-2011"},{"RegDate":"13-10-2011"},{"RegDate":"13-10-2011"},{"RegDate":"13-10-2011"},{"RegDate":"25-10-2011"},{"RegDate":"25-10-2011"},{"RegDate":"03-11-2011"},{"RegDate":"03-11-2011"},{"RegDate":"11-11-2011"},{"RegDate":"16-11-2011"},{"RegDate":"18-11-2011"},{"RegDate":"21-11-2011"},{"RegDate":"02-12-2011"},{"RegDate":"02-12-2011"},{"RegDate":"12-12-2011"}];
var ary = new Array();
for (foo in x) {
    ary.push(x[foo].RegDate);
}
console.log(ary);

jsFiddle example.

I noticed you tag jquery in here as well so, here's a jquery solution:

http://jsfiddle.net/aztechy/DK9KM/

var foo = [
  {"RegDate":"31-03-2011"},
  {"RegDate":"29-07-2011"},
  {"RegDate":"09-08-2011"},
  {"RegDate":"09-08-2011"}
];

var datesArray = [];
$.each(foo, function() {
  datesArray.push(this.RegDate);
});

console.log(datesArray);​

Try This, it may works.

var unavailableDates = [];
for(var i = 0; i < unavailableDates1.length; i++){
    unavailableDates[i] = unavailableDates1[i].RegDate;
}