我有一个服务/工厂,它接受一系列日期,并根据ISO周将其分组到数组[[],[]]
中。此功能按预期工作。
但是如何扩展服务/工厂以获取另一个值数组(只是普通的整数值),并且创建每个日期和值的键值对。仍然采用数组数组的相同输出格式?
这是我的服务/工厂电话:
$scope.weeks = GroupDateRangeService.createArray(timeline_data, time_trend);
传递给服务的数据如下所示:
timeline_data: ["2017-01-04","2017-01-05","2017-01-06","2017-01-07","2017-01-08","2017-01-09","2017-01-10","2017-01-11"]
time_trend: [566, 607, 430, 357, 277, 591, 711, 206]
结果如下所示:
result: [["2017-01-04": 566,"2017-01-05": 607,"2017-01-06": 430,"2017-01-07": 357,"2017-01-08": 277], ["2017-01-09": 591,"2017-01-10": 711,"2017-01-11": 206]]
这是我的服务/工厂:
(function () {
'use strict';
angular
.module('portalDashboardApp')
.factory('GroupDateRangeService', GroupDateRangeService);
GroupDateRangeService.$inject = [];
function GroupDateRangeService() {
var service = {
createArray: createArray,
};
return service;
function createArray(data) {
var arr = data.map(function (s) {
var week = getWeekNumber(parseISOLocal(s));
return week[0] + ('0' + week[1]).slice(-2) + ':' + s;
}).sort();
return createGroupedArray(createGroupObject(arr));
};
function createGroupObject(arr) {
var groupedObj = arr.reduce(function (result, value) {
var b = value.split(':');
if (!result[b[0]]) result[b[0]] = [];
result[b[0]].push(b[1]);
return result;
}, {});
return groupedObj;
};
function createGroupedArray(groupedObj) {
// Grab arrays in order of week number. Sort keys to maintain order
var groupedArray = Object.keys(groupedObj).sort().map(key=>groupedObj[key]);
return groupedArray;
}
/* Helper to get the ISO week number of a date
** @param {Date} date to get week of
** @returns {Array} [year, weekNumber]
*/
function getWeekNumber(d) {
d = new Date(+d);
d.setHours(0, 0, 0, 0);
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
var yearStart = new Date(d.getFullYear(), 0, 1);
var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
return [d.getFullYear(), weekNo];
}
/* Parse ISO 8601 format date string to local date
** @param {string} s - string to parse like 2016-12-15
** @returns {Date}
*/
function parseISOLocal(s) {
var b = s.split(/\D/);
return new Date(b[0], b[1] - 1, b[2]);
}
}
})();
感谢您的指导!
答案 0 :(得分:1)
认为这是您的合并例程(更新以显示如何使用内部与提供的数组,以及如何切换处理指令):
// These arrays were already made by your service
let premadeKeys = [ 'pre', 'made', 'keys' ];
let premadeVals = [ 'val1', 'val2', 'val3' ];
// Formatrules is a naive way to switch how you want it processed, could be any enum etc.
function mergeArrays ( keys = premadeKeys, values = premadeVals, formatRules = 0 ) {
switch ( formatRules ) {
case 0:
return keys.map ( ( d, i ) => {
let obj = {};
obj [ d ] = values [ i ];
return obj;
} );
case 1:
return keys.map ( ( d, i ) => {
let obj = {};
obj [ d ] = values [ i ] + '_TAIL_' + i;
return obj;
} );
}
}
let arr1 = [ "a", "b", "c" ];
let arr2 = [ 1, 2, 3 ];
// provided arrays
console.log ( mergeArrays ( arr1, arr2 ) );
console.log ( mergeArrays ( arr1, arr2, 1 ) );
// pre made arrays
console.log ( mergeArrays ( ) );
console.log ( mergeArrays ( undefined, undefined, 1 ) );
答案 1 :(得分:1)
您的结果看起来不像那样,数组不能有键值对。它必须是一个对象数组的数组。如果您重构函数以获取第二个参数,则可以执行以下操作:
function createArray(data, optionalTimeTrend) {
var arr = data.map(function (s) {
var week = getWeekNumber(parseISOLocal(s));
return week[0] + ('0' + week[1]).slice(-2) + ':' + s;
}).sort();
var results = createGroupedArray(createGroupObject(arr));
if (optionalTimeTrend && optionalTimeTrend.length === data.length) {
for (var i = 0; i < results.length; i++) {
results[i] = results[i].map(function (date, index) {
var obj = {};
obj[date] = optionalTimeTrend[index];
return obj;
});
}
}
return results;
};
您的结果应如下所示:
result: [[{"2017-01-04": 566},{"2017-01-05": 607},{"2017-01-06": 430},{"2017-01-07": 357},{"2017-01-08": 277}], [{"2017-01-09": 591},{"2017-01-10": 711},{"2017-01-11": 206}]]