1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Array question

Discussion in 'JavaScript' started by John Wade, Jan 6, 2018.

  1. #1
    Soooooooo been working this a couple days and for some reason I am probably making it harder then it has to be.......

    var result = JSON.parse(JSON.stringify(result.feed.entry));
                            for(var i = 0; i < result.length; i++){
                            var results = new Array();
                            var obj = {};
                            var tube = result[i];
                             obj["tLink"] = tube.link[0].$.href;
                             obj["title"] = tube.title[0];
                             obj["id"] = tube['yt:videoId'][0];
                             obj["pic"] = tube['media:group'][0]['media:thumbnail'][0].$.url;
                       
                        results.push(obj);
                    console.log(results);
    Code (JavaScript):
    this returns:
    [Object]
    [Object]
    [Object]
    [Object]
    [Object]

    What I'd like it to return is:
    (5)[Object, Object, Object, Object, Object]

    Any help would be greatly appreciated!!!!!
    Photo attached!
     

    Attached Files:

    Solved! View solution.
    John Wade, Jan 6, 2018 IP
  2. #2
    It's all in where you place your closing }. I don't see one between the push and the log, when you'd want the log AFTER. You are also ERASING results inside the loop every loop, that should be set BEFORE the loop! I'd also suggest a name like 'entries' instead of 'result' just for code clarity.

    
    var
    	entries = JSON.parse(JSON.stringify(result.feed.entry)),
    	results = [];
    
    for (var i = 0; i < entries .length; i++) {
    	var tube = entries[i];
    	results.push({
    		'tLink' : tube.link[0].$.href,
    		'title' : tube.title[0],
    		'id' : tube['ty:videoId'][0],
    		'pic' : tube['media:group'][0]['media:thumbnail'][0].$.url
    	});
    }
    
    console.log(results);
    
    Code (markup):
    Side note, if your entries array contains no values that evaluate to loose false, you could simplify this even further:

    
    var
    	entries = JSON.parse(JSON.stringify(result.feed.entry)),
    	results = [];
    
    for (var i = 0, entry; entry = entries[i]; i++) results.push({
    	'tLink' : entry.link[0].$.href,
    	'title' : entry.title[0],
    	'id' : entry['ty:videoId'][0],
    	'pic' : entry['media:group'][0]['media:thumbnail'][0].$.url
    });
    
    console.log(results);
    
    Code (markup):
    Sneaky trick, executes WAY faster, but only works if all your Array entries are loose true. It encounters anything that is loose false (null, empty string, numeric zero) it will stop right there. It's actually a WAY more effective way to loop through the results of operations like queryAll or getElementsByTagName since the nodeList entries are always objects. You try to access past the end of the nodeList, you get null.
     
    deathshadow, Jan 6, 2018 IP
  3. John Wade

    John Wade Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    WOW that is fantastic..... I see what you're saying and why I was having problems doing it.. had the right idea just executing it wrong..... clearing my array and rewriting it with a new object. My for loop was also a bit off and I needed to push at the very end instead which would write all my objects to one array instead of creating new array for each object, got it!!

    Fantastic!! They adhere to the loose true so I'll stick with the second one. I've put them side by side..... mine-yours and I can see where I was going wrong big time.

    I can't Thank you enough! This is really great! I'm quite happy though, I feel like I've learned a lot. I know I have a lot more to learn but I'm getting closer!

    Had to fix the
    'id' : entry['ty:videoId'][0]
    but that was my error and not yours.......it's fixed and returns perfectly!!!

    Again, HUGE Thanks and I love your avatar!!! LOL
     
    John Wade, Jan 6, 2018 IP