gpt4 book ai didi

javascript - 扩展 Angular 服务/工厂以获取日期数组和整数数组,并创建一个包含键值对的数组

转载 作者:行者123 更新时间:2023-11-29 21:11:37 25 4
gpt4 key购买 nike

我有一个服务/工厂,它接受日期数组,并根据 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]);
}
}

})();

非常感谢您的指导!

最佳答案

你的结果不能像那样,数组不能有键值对。它必须是对象数组的数组。如果重构函数以接受第二个参数,则可以执行以下操作:

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}]]

关于javascript - 扩展 Angular 服务/工厂以获取日期数组和整数数组,并创建一个包含键值对的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41591662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com