I am returning data from an api using the getJSON method. The api is from last.fm, and here is the code I have below...
$.getJSON('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=ba2641c3bcc4d9e90ff25d4a7414f4ec&mbid='+lastresult1mbid+'&format=json', function (infodata) {
console.log(infodata);
var lastrel1 =(JSON.stringify(infodata.album.releasedate));
var releasedate1 = $.parseJSON(lastrel1);
var fmtracks =(JSON.stringify(infodata.album.tracks.track[0].name));
var tracks1 = $.parseJSON(fmtracks);
$('#lastfminfo1').append(""+releasedate1+"");
$('#tracks1').append(""+tracks1+"");
});
For the varible fmtracks there are 15 tracks in this album I need to display, so how can I output them all to the lastmininfo1 div?
Track[0].name will work, as will track[1].
I need a loop or [i] or a each function to display them all but everything I am trying will not work.
Help or a point in the write direction would be highly appreciated, Thanks in advance
$.getJSON('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=ba2641c3bcc4d9e90ff25d4a7414f4ec&mbid=d63d427d-499c-4eb4-996e-535e36013766&format=json', function (infodata){
console.log(infodata);
var trackList = infodata.album.tracks.track
for(var i = 0, currentTrack; currentTrack = trackList[i]; ++i){
$("#lastfminfo1").append(currentTrack.name);
}
})
This works, you just need to loop through the tracks and append any information you want from them. This just does the name without any tags.
JSFiddle: http://jsfiddle.net/klatzkaj/3Z884/1/
Yep, you'll have to loop over them, or when supporting IE<9 isn't an issue you could use the forEach method on tracks and push each title to an array.
Since you're using jQuery, however, you may want to look into $.map().
Related
I have a JSON response from a server, which returns me a array with 32 objects (in this case). Something like this:
[{object1},{ object2},{ object3}, etc].
Each object have some info that I use to populate an html template. For that, I just use a simple loop:
for(var i = 0; i < api_empresaListar.length; i++)
{
var item = api_empresaListar[i];
var htmls;
htmls = $('...lots of html code');
...
Then it’s just a simple matter of finding/changing the values, and append items on the DOM. Everything works fine. BUT, for some next parts of the code, I would like to access all the info from the object I used to build the html elements (I just show part of the info). So, after searching a lot, I tried to use data, like this:
var tp = htmls.find(".rl_grupo"); // the main div of each html element created in the loop
$(tp).data('key', api_empresaListar[i]); // here, I expected to just insert the object data in each created item.
But when I try it in the console, I got the object info as expected, but always from the last element in the array. Why is that happening? I believe it might be something stupid, but I can’t figure it out.
So, any ideas on how to solve this, or another method to make this work is appreciated. I made it work by setting some "display:none" placeholder html tags and populate those with the info I need later, but looks like a poor solution...
You should not set your htmls variable in the loop. I think that you crush its content every turn, that's why you only have the last item. You should do something like this:
var htmls = $('<div></div>');
for(var i = 0; i < api_empresaListar.length; i++) {
htmls.append($('...lots of html code'));
}
How about setting an index number on each element inside of your html creating code, then iterating over the $('.rl_grupo') elements, like this?
$('.rl_grupo').each(function(){
var index = $(this).data('index');
var currentData = api_empresaListar[index];
$(this).data('key', currentData);
})
This question relates directly to Highcharts, but is really more pertinent to general JavaScript and loops.
My code is as follows:
load:function(){
var series0 = this.series[0];
var series1 = this.series[1];
setTimeout(function(){
series0.addPoint(myjson.list[0].value);
series1.addPoint(myjson.list[1].value);
}, 1000);
}
I wanted to first show an example of code that works. With Highcharts, this code gathers the designated indexes from my JSON lists and appends them to my existing chart.
When attempting a for loop to perform the same action however I end up bungling it.
My for loop attempt:
var update = [];
for (i = 0; i < myjson.list.length; i++){
update[i] = this.series[i];
update.addPoint(myjson.list[i].Printvalue);
}
There is clearly something wrong with my loop logic, and yet I am unable to figure out exactly what. When running this loop code I get an error of:
update.addPoint is not a function
My biggest guess is it has to do with the way I am handling the this instance.
In your example, update is a normal array because you declare it with var update = []. The normal JavaScript array does not have a function called addPoint.
I'm not sure what it should be, but it definitely doesn't have anything to do with your use of this.
If the items in this.series include addPoint, you might want to use this:
update[i].addPoint(myjson.list[i].Printvalue);
Note the [i] after update.
This is my controller code:
var domainList=angular.copy($scope.busdomain);
if(domainList!=null){
domainList[0].childNode.sort();
//For finding out the length of list
for (var i = 0; i < domainList[0].childNode.length; i++) {
//Here I want to add a check that I can get name only of objects in my list
//Please see the attached Object screenshot for better understanding
if (domainList[0].childNode[i].name!=null) {
var name=domainList[0].childNode[i].name;
domainList[0].childNode.splice(i,1,name);
$scope.busdomainname=domainList[0].childNode;
}
}
I want to get the list of object and take out its name and add to $scope.busdomainname as I need to display only name on another page.
Is there any way to do this. Please help me out. Thanks in advance
Try underscore library example instead of going the hard way. It provides useful functions like this. Your case
_.pluck (domainList, "name");
new here and hit a roadblock, been searching but can't find the answer with my skill set. Task is pretty simple, I want to parse this http://data.sparkfun.com/output/AJ2p4r8Owvt1MyV8q9MV.json which is from a weather station. I have used the W3C tutorial but just can't seem to parse this file, but http://json.parser.online.fr has no problem. All the looping parse examples just give me alert after alert.
All I want is the ability to select temp[0] (out of god knows how many) for example via javascript and have it display on a website. I'm really lost, tried searching and if I've missed the goldmine then my bad. Thanks!
Example code
var text = '[{"humidity":"42.8000","stationtime":"2014-07-06 19:43:52","temp":"23.3000","timestamp":"2014-07-06T09:44:07.918Z"},{"humidity":"43.0000","stationtime":"2014-07-06 19:42:57","temp":"23.2000","timestamp":"2014-07-06T09:42:22.003Z"},{"humidity":"43.2000","stationtime":"2014-07-06 19:42:36","temp":"23.3000","timestamp":"2014-07-06T09:42:51.737Z"}]';
var obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.temp[0];
First, you need to parse the incoming string as below:
temp_arr = JSON.parse(json_string);
Just loop over the temp_arr array, and in each iteration of loop you'll have one object (tobj). For example, like this:
{"humidity":"40.9000","stationtime":"2014-07-06 21:21:03","temp":"22.6000","timestamp":"2014-07-06T11:20:27.231Z"}
All you have to do is, access it like tobj.temp and use it to display on page.
I have written a jquery implementation at: http://jsfiddle.net/DNH5n/2/
Jquery makes working with JSONP much easier heres an example (http://jsfiddle.net/icodeforlove/9mBsr/)
$.getJSON('http://data.sparkfun.com/output/AJ2p4r8Owvt1MyV8q9MV.json?callback=?', function (data) {
data.forEach(function (item) {
$('body').append(JSON.stringify(item));
});
})
update again
heres another example using your code (http://jsfiddle.net/icodeforlove/9mBsr/2/)
var text = '[{"humidity":"42.8000","stationtime":"2014-07-06 19:43:52","temp":"23.3000","timestamp":"2014-07-06T09:44:07.918Z"},{"humidity":"43.0000","stationtime":"2014-07-06 19:42:57","temp":"23.2000","timestamp":"2014-07-06T09:42:22.003Z"},{"humidity":"43.2000","stationtime":"2014-07-06 19:42:36","temp":"23.3000","timestamp":"2014-07-06T09:42:51.737Z"}]';
var obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj[0].temp;
Attempting to build a resume creator as a project for codeacademy.
I'm using a button to "save" the user's input to an array so it can later be appended into the resume.
However, I'm failing at getting the data to "save" to the array. I've looked at similar questions here on stackoverflow and I cannot for the life of me figure out what I am doing wrong.
here's my fiddle
specific code block I'm having trouble with:
$('#experiencesave').click(function(){
for (var i = 0; i < jobs; i++){
jobtitle.push = $('#jobtitle'+i).val();
}
$('#morejobs').append(jobtitle);
});
Well, .push [MDN] is a function which has to be called:
jobtitle.push($('#jobtitle'+i).val());
As an alternative solution, instead of using a for loop, you might want to use .map to collect the values:
var jobtitle = $('input[id^=jobtitle]').map(function() {
return this.value;
}).get();
I don't see a reason to give each of those input elements an ID though. Just give them a class. That makes it a bit easier to bulk-process them later. E.g. the selector could then just be $('input.jobtitle').