1

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.

7 Answers 7

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@brice: Consider I have data with a backslash in it. PHP escapes it like: {"blah":"some\\backslash"} and it goes into the string like: '{"blah":"some\\backslash"}' and the result is an object with a property blah that doesn't contain a backslash and a b, but rather a backspace control character. If the backslash happens to be at the end of the string, everything blows up. If there's an apostrophe anywhere in the JSON, everything blows up.
@minitech Isn't the point of paresJSON to gracefully handle issues such as that? On top of that we all know eval is evil etc. but when you control the data people overreact about its use (not to say you shouldn't avoid it if possible, but...). I do agree the parseJSON is completely unnecessary, though.
@minitech +1 for the great examples, by dangerous I thought you meant something malicious was going to happen.
1
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/

Comments

0

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);
};

Comments

0

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.

Comments

0

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);​

Comments

0

Try This, it may works.

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.