sách gpt4 ai đã đi

javascript JSON Nâng cao

In lại 作者:行者123 更新时间:2023-11-28 02:24:54 27 4
mua khóa gpt4 Nike

我找不到错误。也许你可以帮助我:我的代码如下:

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"}]};

将此代码复制并粘贴到:http://json.parser.online.fr/

下面的代码是有效的。

  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(function(p){

var el = new Element('div.preview'),
name = new Element('h3', {'html': '' + p.config.name + ''}).inject(el),
desc = new Element('span', {'html': p.config.description}).inject(name, 'after');
el.inject(container);

});

附注

如果我将代码编辑为:

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



var el = new Element('div.preview'),
name = new Element('h3', {'html': '' + p.config[i].name + ''}).inject(el);
el.inject(container);

});

它将仅返回 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': '' + p.name + ''}).inject(el);

el.inject(container);

});

1 Câu trả lời

工作代码如下:

for (var i=0;i

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

var el = new Element('div.preview'),
name = new Element('h3', {'html': '' + p.name + ''}).inject(el);
el.inject(container);

});
}

Cảm ơn.

关于javascript JSON 高级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15113201/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com