samedi 2 juillet 2016

jQuery to retrieve JSON items based on items in array


I have a JSON file (names.json) with 300 location ID:Name pairs eg: { "stationNames": [ { "stationID":"01234" ,"stationName":"Station One" }, { "stationID":"09999" ,"stationName":"Station Two" }, { "stationID":"00300" ,"stationName":"Station Three" }] } And a text file (latest.txt) with a list of image names: ID01234_1_201.jpg ID01234_1_291.jpg ID01234_1_090.jpg ID01234_1_180.jpg ID00300_1_270.jpg ID00300_1_360.jpg ID00300_1_090.jpg ID00300_1_180.jpg Where the first part of the image name corresponds to a stationID from the JSON. I need to generate a list of unique locations that match the images in the list. So for the supplied image list the output would be: Station One Station Three So far I have: $.get('latest.txt', function (data) { cleanData = data.replace(/ID/g, "").replace(/.jpg/g, "") + ""; camImages = cleanData.split("n"); $.each(camImages, function (key, value) { part = value.split("_"); station = part[0]; cameraNo = part[1]; angle = part[2]; if (stationTmp !== station) { stations.push(station); stationTmp = part[0]; } if (station === stationID) { $('body').append($('<div><image src="images/ID' + station + '_' + cameraNo + '_' + angle + '.jpg" width=300 /></div>')); //console.log(station) } }); getParts(stations); }); function getParts(camStationID) { $.getJSON('names.json', function (json) { $.each([json.stationNames, camStationID], function () { $.each(this, function (i, v) { console.log(v); }); }); }); }; Can anyone point me in the right direction?

Aucun commentaire:

Enregistrer un commentaire