问题描述
您好,所以我可以获得两个单独的JSON请求并分别吐出数据。我无法弄清楚的是如何将来自一个JSON数组的数据组合到另一个JSON数组中。下面是调用两个JSON数组的JS,其目的是能够匹配两个数组中的日期,并在日历上的每一天吐出目标数据(转换,完成,值)。所有数据都应添加到citationHTML变量中。
Hello, so I am able to get two separate JSON requests and spit out the data separately. What I cannot figure out is how to combine the data from one JSON array to the other. Below is the JS that calls both JSON arrays, the idea is to be able to match the dates from both arrays and spit out the goal data (conversions, completions, value) on each day on the calendar. All of the data should be added to the citationHTML variable.
var zipcode = '27560';
var appid = '96afa96cadeb7165258ae95b77fdc';
var startdate = '2015-09-01';
var enddate = '2015-09-30';
var timeperiod ='24';
var dataUrl = '//api.worldweatheronline.com/premium/v1/past-weather.ashx?q='+ zipcode +'&format=json&date='+ startdate +'&enddate='+ enddate +'&tp='+ timeperiod +'&key='+ appid
var dataUrl2 = '//westbrookfl.com/wp-content/plugins/CSAnalytics/lib/data/data-GoalValueTotals.php'
//Creates Table for Citation Data
$.when(
$.getJSON(dataUrl),
$.getJSON(dataUrl2)
).done (function (data, data2) {
var data = data[0].data;
console.log(data, data2);
var citationHTML = '';
var channelHTML = '';
jQuery.each(data, function (i) {
var weather = data.weather;
for (var i = 0; i < weather.length; ++i) {
citationHTML += '<li id="day'+[i]+'" class="day"><div class="date">' + weather[i].date + '</div><div class="svg-icon"><img src="' + weather[i].hourly[0].weatherIconUrl[0].value + '" /></div><div class="data-wrap col2"><p class="data hi-temp"><span>' + weather[i].maxtempF + '</span><sup class="deg ng-scope" data-ng-if="hasValue()">°</sup></p><p class="data lo-temp"><span>' + weather[i].mintempF + '</span><sup class="deg ng-scope" data-ng-if="hasValue()">°</sup></p></div><p class="data desc">' + weather[i].hourly[0].weatherDesc[0].value + '</p></li>';
}
})
jQuery.each(data2[0], function (i, item) {
channelHTML += '<tr><td class="tg-yw4l">' + item.date + '</td><td class="tg-yw4l">' + (+item.goalConversionRateAll).toFixed(2) + '%</td><td class="tg-yw4l">' + item.goalCompletionsAll + '</td><td class="tg-yw4l">$' + item.goalvalueall + '</td></tr>';
})
jQuery('#citation_report').append(citationHTML);
jQuery('#channeldata_table').append(channelHTML);
});
这里也是小提琴:https://jsfiddle.net/joseph_a_garcia/s41kr5hj/52/
Here is fiddle for it as well: https://jsfiddle.net/joseph_a_garcia/s41kr5hj/52/
推荐答案
这篇关于将JSON数据组合到一个jquery变量上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!