String returns as NaN

I have this code am playing with.

$.get("https://congress.api.sunlightfoundation.com/committees?apikey=******", function (response) {
	
			console.log(response);
			
			if(response.results && response.results.length > 0){
					var $results = $("#subSect1");
					var bills = "<p> The bills: </p>";
					
					$.each(response.results, function(i, rep){
						if('senate'=== rep.chamber.toLowerCase()){
							bills += "<div class=\"bill\">"+ rep.committee_id +"<br/>";
							bills += +rep.name+"<br/>";
							bills += +rep.parent_committee_id+"<br/>";
							bills += +rep.subcommittee+"<br/>";
							bills += "</div>";
						}
					});
					bills += '<p> Write em</p>';
					$results.html(bills);
					
				}else{
					//$results.html('<p> none found, no bills for you</p>');
				}

		}, "jsonp");

It works as it returns the data. but it gives me NaN for the rep.name & rep.parent_committee _id. I see no reason as to why. A similar loop does returen strings for names and last names.
thx

It’s the +rep. That’s called a unary plus, which is trying to convert the value in the variable into a numeric value.

Change the +rep to rep (or even just add a space between the + and rep) and you should be fine.

1 Like

Dead on thx.


bills += "<div class=\"bill\">"+ rep.committee_id +"<br/>";
							bills +=   rep.name+"<br/>";
							bills +=   rep.parent_committee_id+"<br/>";
							bills +=   rep.subcommittee+"<br/>";

@DaveMaxwell I want to re-write this as a for loop, just to try it. Do you see any crazy errors here? seems like it is correct…


for(i=0; i<response.results.length; i++){
	if('senate'=== rep.chamber.toLowerCase()){
	bills += "<div class=\"bill\">"+ rep.committee_id +"<br/>";
	bills +=   rep.name+"<br/>";
	bills +=   rep.parent_committee_id+"<br/>";
	bills +=   rep.subcommittee+"<br/>";
	bills += "</div>";
			}
}

You’re not initializing rep there, so you’re going to get the same information (probably nothing unless you initialize rep somewhere else) for the number of instances in the response.results object.

ok will try it out w/a diff stream of info
thx

I think I am getting closer, but…when i run this i do see the results in the console. But not when i try to see in the page.

$.get("https://congress.api.sunlightfoundation.com/bills?apikey=****, function (response) {
	console.log(response);
	var i=0;
	var html = '';
	for(i=0; i<response.results.length; i++){
			
		var objList = response.results[i];
		console.log(objList);
		html += objList[i] + '<br />';
			
	$("#ct0").append(html);
     }

    }, "jsonp");

You can’t just use objList[i]. It’s an array. You need to pull the specific elements out of it…

ha…got it

	html += objList.chamber + '<br />';

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.