简体   繁体   English

foreach for javascript,json数组

[英]foreach for javascript, json array

I have a JSON array that looks like this: 我有一个看起来像这样的JSON数组:

[{"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: 获取此json数组的代码如下:

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. 我不确定如何执行此操作,我尝试了一次foreach,但未成功。

All help will be appreciated. 所有帮助将不胜感激。

First of all, that parseJSON is unnecessary and actually dangerous. 首先, parseJSON是不必要的,实际上是危险的。 Take it out: 把它拿出来:

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

Then, just use jQuery.map : 然后,只需使用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:意外的输入结尾(单转义转义字符串的结尾)
  • SyntaxError: Unexpected identifier (Unescaped single quote breaks out of the string, actually an XSS vulnerability ) SyntaxError:意外的标识符(不带引号的单引号引起来的字符串, 实际上是XSS漏洞
  • Object 宾语
    key: "Someackslash" ( \\\\b becomes the \\b backspace control character) 密钥:“ Someackslash”( \\\\b成为\\b退格控制字符)
    __proto__: Object __proto__:对象

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. 有效的JSON始终是有效的JavaScript对象,您可以相信自己的PHP不在其中包含注入代码。

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. 每个对象({'RegDate:'date'})都是javascript数组中的一个项目。 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"} . 因此要获取第一项将是theArray[0]这将为您提供{"RegDate":"31-03-2011"} Then to get that particular date string you just have to use the key theArray[0]['RegDate'] !. 然后,要获取该特定日期字符串,只需使用键theArray[0]['RegDate'] !。 So since you want to go through every member of a list, you should get the list length using .length . 因此,由于要遍历列表的每个成员,因此应使用.length来获取列表长度。

For loops are usually used for this type of iteration and not for..in. For循环通常用于这种类型的迭代,而不用于..in。 because for in can access properties that are undesired! 因为for可以访问不需要的属性! http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/ http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/

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: 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 . jsFiddle示例

I noticed you tag jquery in here as well so, here's a jquery solution: 我注意到您也在这里标记了jquery,所以这是一个jquery解决方案:

http://jsfiddle.net/aztechy/DK9KM/ 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;
}
for(var i=0;i<unavailableDates.length;i++) {
    unavailableDates[i] = unavailableDates[i].RegDate;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM