开发者

jQuery $.each() - Array of Objects

开发者 https://www.devze.com 2023-02-13 23:39 出处:网络
I am trying to perform jQuery each function on something like: \"RelatedDoc\": [ { \"Id\": \"test\",开发者_如何学Python

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

"RelatedDoc": [
    {
        "Id": "test",开发者_如何学Python
        "Number": "26262316"
    }
],

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

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


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


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);
    });
});


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号