0

I am attempting to loop through the following JSON object without success:

[
    {
        "Count": 0,
        "GroupId": 1,
        "Icon": null,
        "Service": [
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 1,
                "Type": "Cat"
            },
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 2,
                "Type": "Dog"
            },
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 3,
                "Type": "Whale"
            }
        ],
        "Type": "Animal carcass removal"
    }
]    

I need to get to the 'Service' object and append the Type to a dropdown list? I suspect I may need a each loop within a loop by cant seem tog et it right?

Any help would be appreciated.

Thanks

1
  • Please post the Javascript you already had so we have a basis to answer you. Commented Mar 25, 2014 at 11:54

5 Answers 5

1

You should be able to do it with

$.each(data[0].Service, function () {
    $('select').append('<option val="'+this.Type+'">'+this.Type+'</option>')
});

where data is represents the object from your question.

See live fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

By fallback, if no value is specified the title text is taken as value. And the OP was having problems with the looping (as I understood it), but I've altered the answer to be more explicit.
0

Try like this

            var data = [
    {
        "Count": 0,
        "GroupId": 1,
        "Icon": null,
        "Service": [
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 1,
                "Type": "Cat"
            },
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 2,
                "Type": "Dog"
            },
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 3,
                "Type": "Whale"
            }
        ],
        "Type": "Animal carcass removal"
    }
]

        for (i = 0; i < data[0].Service.length; i++) {
            $("#slct").append($("<option/>", { html: data[0].Service[i].Type, value: data[0].Service[i].ServiceId }));
        }

Html

<select id="slct">
</select>

Comments

0
$.each(a[0].Service,function (i,n){

  $("#ddl").append($("<option></option>").val(n.ServiceId).text(n.Type));


})

http://jsbin.com/gigusora/2/edit

Comments

0

Try like this

var jso=[
{
    "Count": 0,
    "GroupId": 1,
    "Icon": null,
    "Service": [
        {
            "Count": 0,
            "Icon": null,
            "ServiceId": 1,
            "Type": "Cat"
        },
        {
            "Count": 0,
            "Icon": null,
            "ServiceId": 2,
            "Type": "Dog"
        },
        {
            "Count": 0,
            "Icon": null,
            "ServiceId": 3,
            "Type": "Whale"
        }
    ],
    "Type": "Animal carcass removal"
}

] 
var ser=jso[0].Service;
var output='';
for(var i in ser)
{
   output+='<option value="'+ser[i].Type+'">'+ser[i].Type+'</option>';
}
$('select').append(output)

DEMO

1 Comment

what about val for the option ??
0

try it:

var data = [{
"Count": 0,
    "GroupId": 1,
    "Icon": null,
    "Service": [{
    "Count": 0,
        "Icon": null,
        "ServiceId": 1,
        "Type": "Cat"
}, {
    "Count": 0,
        "Icon": null,
        "ServiceId": 2,
        "Type": "Dog"
}, {
    "Count": 0,
        "Icon": null,
        "ServiceId": 3,
        "Type": "Whale"
}],
    "Type": "Animal carcass removal"
}];

for(var d in data){
   var service = data[d].Service;
   for(var i = 0;i < service.length;i += 1){
       console.log(service[i].type);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.