Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript JSON高级_Javascript_Json_Object_Mootools_Each - Fatal编程技术网

javascript JSON高级

javascript JSON高级,javascript,json,object,mootools,each,Javascript,Json,Object,Mootools,Each,我找不到窃听器。也许你能帮助我: 我的代码如下: var data = {"product":[{"config":[{"id":"1","price":"100","sku":"10548796345","manufacturer":"Apple","name":"Product 1", "description":"Web site has two parts: the Site (which is what your site visitors see) and the Administ

我找不到窃听器。也许你能帮助我: 我的代码如下:

var data  = {"product":[{"config":[{"id":"1","price":"100","sku":"10548796345","manufacturer":"Apple","name":"Product 1", "description":"Web site has two parts: the Site (which is what your site visitors see) and the Administrator (which is where you will want to do a lot of the site management). You need to log in to the Administrator separately. There is a link to the administrator on the top menu that you will see when you log .","cid":"1","qty":"102"}],"options":[{"color":[{"blue":"+10","count":"2"},{"red":"+20","count":"3"}]},{"size" :[{"S":"+10","count":"1"},{"M":"+20","count":"4"},{"L":"+30","count":"5"},{"XL":"+40","count":"2"}]}]},{"config":[{"id":"2","price":"100","sku":"10548796341","manufacturer":"Apple","name":"Product 2", "description":"Web site has two parts: the Site (which is what your site visitors see) and the Administrator (which is where you will want to do a lot of the site management). You need to log in to the Administrator separately. There is a link to the administrator on the top menu that you will see when you log in.","cid":"1","qty":"102"
}],"options":[{"color":[{"blue":"+10","count":"2"},{"red":"+20","count":"3"}]},{"size" :[{"S":"+10","count":"1"},{"M":"+20","count":"4"},{"L":"+30","count":"5"},{"XL":"+40","count":"2"}]}]}],"categories":[ {"id":1,"name":"Category 1", "description":"Category 1 description"}, {"id":2,"name":"Category 2", "description":"Category 2 description"}, {"id":3,"name":"Category 3", "description":"Category 3 description"}]};
for (var i=0;i<data.product.length;i++) {

                data.product[i].config.each(function(p){     

                     var el = new Element('div.preview'),
                         name = new Element('h3', {'html': '<a href="#!product/product?product_id='+parseInt(p.id)+'">' + p.name + '</a>'}).inject(el);
                         el.inject(container);  

                 });
          }
将此代码复制并粘贴到:

下面的代码是工作

  data.categories.each(function(c){
      var opt = new Option(c.name, c.id);               
      try {category_selector.add(opt, null)} catch (err) {category_selector.add(opt)}                          
    });
为什么此代码与上面的代码不同(返回未定义):

data.product.each(函数p){
var el=新元素('div.preview'),
name=新元素('h3',{'html':''''}).inject(el),
desc=新元素('span',{'html':p.config.description}).inject(名称'after');
el.注射(容器);
});
附言

如果我将代码编辑为:

data.product.each(function(p, i){



                     var el = new Element('div.preview'),
                         name = new Element('h3', {'html': '<a href="#!product/product?product_id='+parseInt(p.config[i].id)+'">' + p.config[i].name + '</a>'}).inject(el);                         
                         el.inject(container);  

         });
data.product.each(函数(p,i){
var el=新元素('div.preview'),
name=新元素('h3',{'html':''''});
el.注射(容器);
});
它将只返回1个产品和控制台错误:p.config[i]未定义

第2页:

 data.obj[1].config.each(function(p){ 
// [1] - return first product; [2] - return second; How to return all 1 and 2?   

         var el = new Element('div.preview'),
         name = new Element('h3', {'html': '<a href="#!product/product?product_id='+parseInt(p.id)+'">' + p.name + '</a>'}).inject(el);

         el.inject(container);  

             });
data.obj[1].config.each(函数(p){
//[1]-返回第一个产品;[2]-返回第二个产品;如何返回所有1和2?
var el=新元素('div.preview'),
name=新元素('h3',{'html':''''});
el.注射(容器);
});

data.product。每个
将运行回调2次(因为里面有2个对象)。第一次,p将是config对象。第二次,它将是options对象

您正在执行类似于
p.config.id
的操作,当p是“选项”时,这没有意义。 似乎您根本不需要使用每个迭代器?
只需在开始时使用
var p=data.product
,并删除每次迭代的

data.product。每个
都将运行回调2次(因为内部有2个对象)。第一次,p将是config对象。第二次,它将是options对象

您正在执行类似于
p.config.id
的操作,当p是“选项”时,这没有意义。 似乎您根本不需要使用每个迭代器? 只需在开始时使用
var p=data.product
,并删除每次迭代的

工作代码如下:

var data  = {"product":[{"config":[{"id":"1","price":"100","sku":"10548796345","manufacturer":"Apple","name":"Product 1", "description":"Web site has two parts: the Site (which is what your site visitors see) and the Administrator (which is where you will want to do a lot of the site management). You need to log in to the Administrator separately. There is a link to the administrator on the top menu that you will see when you log .","cid":"1","qty":"102"}],"options":[{"color":[{"blue":"+10","count":"2"},{"red":"+20","count":"3"}]},{"size" :[{"S":"+10","count":"1"},{"M":"+20","count":"4"},{"L":"+30","count":"5"},{"XL":"+40","count":"2"}]}]},{"config":[{"id":"2","price":"100","sku":"10548796341","manufacturer":"Apple","name":"Product 2", "description":"Web site has two parts: the Site (which is what your site visitors see) and the Administrator (which is where you will want to do a lot of the site management). You need to log in to the Administrator separately. There is a link to the administrator on the top menu that you will see when you log in.","cid":"1","qty":"102"
}],"options":[{"color":[{"blue":"+10","count":"2"},{"red":"+20","count":"3"}]},{"size" :[{"S":"+10","count":"1"},{"M":"+20","count":"4"},{"L":"+30","count":"5"},{"XL":"+40","count":"2"}]}]}],"categories":[ {"id":1,"name":"Category 1", "description":"Category 1 description"}, {"id":2,"name":"Category 2", "description":"Category 2 description"}, {"id":3,"name":"Category 3", "description":"Category 3 description"}]};
for (var i=0;i<data.product.length;i++) {

                data.product[i].config.each(function(p){     

                     var el = new Element('div.preview'),
                         name = new Element('h3', {'html': '<a href="#!product/product?product_id='+parseInt(p.id)+'">' + p.name + '</a>'}).inject(el);
                         el.inject(container);  

                 });
          }
for(var i=0;i工作代码如下:

var data  = {"product":[{"config":[{"id":"1","price":"100","sku":"10548796345","manufacturer":"Apple","name":"Product 1", "description":"Web site has two parts: the Site (which is what your site visitors see) and the Administrator (which is where you will want to do a lot of the site management). You need to log in to the Administrator separately. There is a link to the administrator on the top menu that you will see when you log .","cid":"1","qty":"102"}],"options":[{"color":[{"blue":"+10","count":"2"},{"red":"+20","count":"3"}]},{"size" :[{"S":"+10","count":"1"},{"M":"+20","count":"4"},{"L":"+30","count":"5"},{"XL":"+40","count":"2"}]}]},{"config":[{"id":"2","price":"100","sku":"10548796341","manufacturer":"Apple","name":"Product 2", "description":"Web site has two parts: the Site (which is what your site visitors see) and the Administrator (which is where you will want to do a lot of the site management). You need to log in to the Administrator separately. There is a link to the administrator on the top menu that you will see when you log in.","cid":"1","qty":"102"
}],"options":[{"color":[{"blue":"+10","count":"2"},{"red":"+20","count":"3"}]},{"size" :[{"S":"+10","count":"1"},{"M":"+20","count":"4"},{"L":"+30","count":"5"},{"XL":"+40","count":"2"}]}]}],"categories":[ {"id":1,"name":"Category 1", "description":"Category 1 description"}, {"id":2,"name":"Category 2", "description":"Category 2 description"}, {"id":3,"name":"Category 3", "description":"Category 3 description"}]};
for (var i=0;i<data.product.length;i++) {

                data.product[i].config.each(function(p){     

                     var el = new Element('div.preview'),
                         name = new Element('h3', {'html': '<a href="#!product/product?product_id='+parseInt(p.id)+'">' + p.name + '</a>'}).inject(el);
                         el.inject(container);  

                 });
          }

for(var i=0;它不是JSON,这是一个对象初始值设定项,哪里有问题?我看不到任何函数返回任何内容。您是否添加了控制台行并查看发生了什么情况?当您发现自己在写“…无法工作…”在一个技术问题中,将其退格,准确地说出您预期会发生什么,确切地说正在发生什么,为什么您认为这是错误的,并引用任何可用的错误报告的输出(例如,所有主要浏览器现在都有一个错误控制台)。这不是JSON,这是一个对象初始值设定项,哪里有问题?我看不到任何函数返回任何内容。是否添加了控制台行并查看发生了什么?任何时候您发现自己在写“…无法工作…”在一个技术问题中,将其退格,准确地说出您预期会发生什么,确切地说正在发生什么,为什么您认为这是错误的,并引用任何可用的错误报告的输出(例如,所有主要浏览器现在都有一个错误控制台)。如果我有很多产品,我如何删除它们?我第一次误读了你的数据,很抱歉。你的每个都很好。但是你似乎希望它返回一些东西。但是Array.forEach(Javascript)和Array.each(mootools)不返回任何内容,因此您通常会看到未定义的结果。如果我有很多产品,我如何删除它们?对不起,我第一次误读了您的数据。您的每个都很好。但是您似乎希望它返回一些内容。但是Array.forEach(Javascript)和Array.each(mootools)是不返回任何内容,因此结果显示未定义是正常的。由于config是一个数组,但有1项,因此可以这样做:由于config是一个数组,但有1项,因此可以这样做: