jQuery $.each() - 对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5157126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:39:28  来源:igfitidea点击:

jQuery $.each() - Array of Objects

jqueryarrays

提问by Coughlin

I am trying to perform jQuery each function on something like:

我正在尝试对以下内容执行 jQuery 每个函数:

"RelatedDoc": [
    {
        "Id": "test",
        "Number": "26262316"
    }
],

Which is a part of a large JSON object. So far I have:

这是大型 JSON 对象的一部分。到目前为止,我有:

$.each($('#panel_MRD').data('obj'), function (key,value) {
    $('select.mrdDisplayBox').addOption( key, value, false);
});

I am trying to get the option to display "ID - NUMBER" - Any ideas? The above displays but not the right format.

我正在尝试获得显示“ID - NUMBER”的选项 - 有什么想法吗?以上显示但不是正确的格式。

回答by Jakub Konecki

$.each(largeJSONobject.ReleatedDoc, function (index,value) {
    $('select.mrdDisplayBox').addOption(value.Id, value.Id + ' - ' + value.Number, false);
});

Your value is the single element from your array: { Id: '', Number: '' }

您的值是数组中的单个元素:{ Id: '', Number: '' }

Documentation is here: http://api.jquery.com/jQuery.each/

文档在这里:http: //api.jquery.com/jQuery.each/

回答by abdul rashid

Here is my problem and what i solved. Use firebug. this is Array Object. Which is use to create four check boxes.

这是我的问题和我解决的问题。使用萤火虫。这是数组对象。用于创建四个复选框。

[
    {"datamet":"1","vchAmenityName":"TV"},
    {"datamet":"2","vchAmenityName":"Cable TV"},
    {"datamet":"5","vchAmenityName":"Internet"},
    {"datamet":"7","vchAmenityName":"Air Conditioning"}
]

<input type="checkbox" value="1" id="datamet1" />
<input type="checkbox" value="2" id="datamet2" />
<input type="checkbox" value="5" id="datamet5" />
<input type="checkbox" value="7" id="datamet7" />

i have to find this inside first. Means to check the element inside it like below [{"datamet":"2"}]

我必须先在里面找到这个。意味着检查它里面的元素,如下所示 [{"datamet":"2"}]

For this i did following .. i got the problem solved

为此我做了以下..我解决了问题

$.each(result, function(idx, obj){ 
    $.each(obj, function(key, value){
        console.log(key + ": " + value);
    });
});

回答by Vadim

$.each($('#panel_MRD').data('obj'), function (key,value) {
    $('select.mrdDisplayBox').addOption( value.Id, value.Id + ' - ' + value.Number, false);
});

回答by jim tollan

option 1(this uses the keyas the option 'id' i.e. 1, 2, 3 etc):

选项 1(这使用key作为选项“id”,即 1、2、3 等):

$.each($('#panel_MRD').data('obj'), function (key,value) {
    $('select.mrdDisplayBox').addOption(key, value.Id + ' - ' + value.Number, false);
});

have not tested, so potentially rushed answer.

还没有测试,所以可能会匆忙回答。

[edit]- had a quick look back at this as i realised that there are potentially 2 values that you could use as the option 'id', either key or value.Number.

[编辑]- 快速回顾一下这个,因为我意识到可能有 2 个值可以用作选项“id”,key 或 value.Number。

option 2(this uses value.Numberas the option 'id' i.e. 26262316):

选项 2(这value.Number用作选项“id”,即 26262316):

$.each($('#panel_MRD').data('obj'), function (key,value) {
    $('select.mrdDisplayBox').addOption(value.Number, value.Id + ' - ' + value.Number, false);
});

will stop thinking now... :-)

现在会停止思考...... :-)