Foreach Ajax Json - Jquery

11,420
{"stars":["Chris Pine","Keira Knightley","Kevin Costner"]}

Change it to the above. You currently have an array inside an array.

Or do

$.each(data.stars[0], function(i, star) {
            $('#stars').append('<input type="text" id="star" value="'+star+'" />');
        });

If you expect to have multiple arrays in the outer array you have to do nested loops

Share:
11,420
user3375691
Author by

user3375691

Updated on June 29, 2022

Comments

  • user3375691
    user3375691 almost 2 years

    Can anyone help me

    I have a Array

    {"stars":[["Chris Pine","Keira Knightley","Kevin Costner"]]}
    

    What i'm trying to do is foreach star i want to append a input to a div and foreach star with they're value inside the input this is what i've so far.

    $.ajax({
        type: "GET",
        url: 'ajax/get_details.php',
        data: {id: imdb_id},
        dataType: 'json',
        success: function(data) {
            $.each(data.stars, function(i, star) {
                $('#stars').append('<input type="text" id="star" value="'+star+'" />');
            });
        },
    });
    

    Can anyone tell me what i'm doing wrong please thanks