1

I am trying to get all the items in an array to show using json and jquery from the song of ice and fire api. I can only get one item to show from each of the arrays.

Here is the codepen: https://codepen.io/frederickalcantara/pen/aWeXOz

var data;

$.getJSON(characters[i], function(json) {
    data = json;

    var alliance = $('#alliance');
    for (var i = 0; i < data.allegiances.length; i++) {
        if (i === data.allegiances.length - 1) {
            $.getJSON(data.allegiances[i], function(json1) {
               alliance.html(json1.name);
            });
        } else {
            $.getJSON(data.allegiances[i], function(json1) {
               alliance.html(json1.name + ', ');
            });
        }

    }

    const title = $('#title');

    if (data.titles.length === "") {
        return 'N/A';
    } else {
        for (i = 0; i < data.titles.length; i++) {

            if (i === data.titles.length - 1) {
                title.html(data.titles[i]);
            } else {
                title.html(data.titles[i]) + ', ';
            }

        }


    const tv = $('#seasons');

    for (var i = 0; i < data.tvSeries.length; i++) {
        if (i === data.tvSeries.length - 1) {
            tv.html(data.tvSeries[i]);
        } else {
            tv.html(data.tvSeries[i] + ', ');
        }
    }

    const actor = $('#actors')
    if (json.playedBy === "") {
        return 'N/A';
    } else {
        actor.html(json.playedBy);
    }

});
4
  • It looks like .html should be .append Commented Jun 1, 2017 at 0:32
  • For which array though? Also, doesn't .append require that you attach an html element to it? I'm not trying to create any html elements, I already did my html on the html page lol. Commented Jun 1, 2017 at 0:33
  • In every for loop that contains .html. Commented Jun 1, 2017 at 0:35
  • .append will only create an HTML element if you pass HTML to it. Plain text is appended without creating an HTML element. (As a side note, if you didn't want to be appending HTML, you probably should have been using .text, but that's not actually what you need). Commented Jun 1, 2017 at 0:40

1 Answer 1

1

The main problem is your loop. You keep replacing the value in the html element until the last value in the array. You can simplify this code like this:

  title.html(data.titles.join(','));

which replaces all of this:

 for (i = 0; i < data.titles.length; i++) {

        if (i === data.titles.length - 1) {
            title.html(data.titles[i]);
        } else {
            title.html(data.titles[i]) + ', ';
        }

    }

Update: Handling the allegiances. Using a Promise here is important because you are making a number of AJAX calls and you need to be sure that they are resolved before attempting to display them. You can replace the entire for loop for the allegiances like this:

  Promise.all(data.allegiances.map(function(ally){
     return new Promise(function(resolve, reject){
         $.getJSON(ally, function(json) {
               resolve(json.name);
         });
     });
   }))
   .then(function(allies){
     alliance.html(allies.join(', '));
   });
Sign up to request clarification or add additional context in comments.

4 Comments

I use the conditional, because the last point in the array will include the comma which is something I don't want.
And the join() takes care of that. MDN Docs
Ok, and what about the allegiance array? Since it uses json to get the name is there anything special I have to do or can I still use join() normally?
For that you may want to consider the map function. Also as you are making AJAX calls in that loop you may need to consider Promises to ensure that they all complete before moving on.

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.