jQuery $.each() - Array of Objects

前端 未结 4 1821
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-15 00:21

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

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


        
相关标签:
4条回答
  • 2021-02-15 00:34
    $.each($('#panel_MRD').data('obj'), function (key,value) {
        $('select.mrdDisplayBox').addOption( value.Id, value.Id + ' - ' + value.Number, false);
    });
    
    0 讨论(0)
  • 2021-02-15 00:41

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

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

    $.each(result, function(idx, obj){ 
        $.each(obj, function(key, value){
            console.log(key + ": " + value);
        });
    });
    
    0 讨论(0)
  • 2021-02-15 00:47
    $.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: '' }

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

    0 讨论(0)
  • 2021-02-15 00:49

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

    $.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.

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

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

    will stop thinking now... :-)

    0 讨论(0)
提交回复
热议问题