Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Array Problem

Options
  • 09-07-2015 10:02am
    #1
    Registered Users Posts: 1,298 ✭✭✭


    Hi Everyone, so i'm working with a returned object from an api and its giving me the comments as long as I access them within a for loop. The only problem with this is that it creates an array for each comment instead of putting them all into one over all array. I know the solution is probably going to be something simple but right now my head is wrecked trying to figure it out. Can anyone spot a way around it? I've attached a screen grab of the returned object and attached the code.


    While the name and id vars are in the code I haven't included them in the screenshot.


    [HTML]
    var coms = feed_comments;
    var coms_data = coms.data;
    for(var a = 0; a < coms_data.length; a++)
    {

    var com_id = coms_data[a].id
    var com_message = coms_data[a].message;
    var com_author = coms_data[a].from;
    var com_author_name = com_author.name;
    var com_author_id = com_author.id;
    var com_likes = coms_data[a].like_count;
    var com_time = coms_data[a].created_time;

    var com_array = [com_id, com_message, com_author_id,
    com_author_name, com_likes, com_time];

    }
    var com_main_array = [];
    com_main_array.push(com_array);
    console.log("MAIN COMMENTS: ",com_main_array);
    [/HTML]

    354687.png



    FOUND THE SOLUTION! The problem in this instance was that the loop was rewriting the main storage array every time. By storing the array before the loop then pushing to it within the loop it stored the data i need in it.

    [HTML]
    var coms = feed_comments;
    var coms_data = coms.data;
    var com_main_array = [];
    for(var a = 0; a < coms_data.length; a++)
    {
    var com_id = coms_data[a].id
    var com_message = coms_data[a].message;
    var com_author = coms_data[a].from;
    var com_author_name = com_author.name;
    var com_author_id = com_author.id;
    var com_likes = coms_data[a].like_count;
    var com_time = coms_data[a].created_time;

    var com_array = [com_id, com_message, com_author_id, com_author_name, com_likes, com_time];
    // console.log("COMS ARRAy: ",com_array);
    com_main_array.push(com_array);

    }
    console.log("MAIN ARRAY:",com_main_array);

    [/HTML]


Comments

  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,240 Mod ✭✭✭✭L.Jenkins


    Are the comments not being returned as objects? It essentially looks like an array of arrays. You could easily just manipulate the initial array to grab the comments and pop it into a new array on its on.


  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    Itzy wrote: »
    Are the comments not being returned as objects? It essentially looks like an array of arrays. You could easily just manipulate the initial array to grab the comments and pop it into a new array on its on.

    With this api i've got a returned object, with an array of objects, with keys that return either a value of another array, in those array are more objects and inside those objects are the data that I need. Haven't really worked with OOP that much in JS so still getting my head around it.


  • Moderators, Society & Culture Moderators Posts: 17,642 Mod ✭✭✭✭Graham


    What are you trying to do with the data that's returned?


  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    Graham wrote: »
    What are you trying to do with the data that's returned?


    Just displaying it on a newsfeed. So posts and comments on the post.


  • Moderators, Society & Culture Moderators Posts: 17,642 Mod ✭✭✭✭Graham


    Is this JSON data that's being returned from an API? If it is, have you looked at JSON.parse?

    Quick example here: http://www.mkyong.com/javascript/how-to-access-json-object-in-javascript/


  • Advertisement
Advertisement