gpt4 book ai didi

javascript - 按键分组对象数组并将分组结果与日期数组进行比较

转载 作者:行者123 更新时间:2023-11-30 20:07:56 24 4
gpt4 key购买 nike

如何将对象数组分组为具有键索引名的数组并与日期数组进行比较

var data= [
{
"indexname": "red",
"data": [
{
"date": "2018-09-07",
"count": 3
}
]
},
{
"indexname": "red",
"data": [
{
"date": "2018-09-05",
"count": 2
}
]
},
{
"indexname": "red",
"data": [
{
"date": "2018-09-06",
"count": 10
}
]
},
{
"indexname": "yellow",
"data": [
{
"date": "2018-09-07",
"count": 6
}
]
}
]
var dates=['2018-08-09','2018-08-07','2018-09-07','2018-09-01','2018-09-06','2018-09-05','2018-09-04','2018-09-03','2018-09-02']
var grouped = _.mapValues(_.groupBy(data.results, 'indexname'),
clist => clist.map(index => _.omit(index, 'indexname')));
required format
var result=[['date','red','yellow'],[2018-08-09,0,0],[2018-08-07,0,0],[2018-09-07,3,6],[2018-09-06,10,0],[2018-09-05,5,0],[2018-09-04,0,0],[2018-09-03,0,0],[2018-09-02,0,0],]

我有一个对象数组和日期数组我想得到以下结果如何实现?

最佳答案

这是您想要的输出结果的纯 javascript 表示。可能会减少 1 或 2 个循环:

var data = [{
"indexname": "red",
"data": [{
"date": "2018-09-07",
"count": 3
}]
},
{
"indexname": "red",
"data": [{
"date": "2018-09-05",
"count": 2
}]
},
{
"indexname": "red",
"data": [{
"date": "2018-09-06",
"count": 10
}]
},
{
"indexname": "yellow",
"data": [{
"date": "2018-09-07",
"count": 6
}]
}
];

var dates = ['2018-08-09', '2018-08-07', '2018-09-07', '2018-09-01', '2018-09-06', '2018-09-05', '2018-09-04', '2018-09-03', '2018-09-02'];

//Prepair index1 of final array an push to see how many colors are present
var index1 = ['date'];

var colors_sorted_arr = [];
var dates_sorted_arr = [];

//Push data to index1 for colors
for (var i = 0; i < data.length; i++) {
if (index1.indexOf(data[i].indexname) == -1) {
index1.push(data[i].indexname)
}
}

//Seperate out data according to color wise
for (var i = 0; i < data.length; i++) {
for (var j = 1; j < index1.length; j++) {
if (data[i].indexname == index1[j]) {
if (colors_sorted_arr[(j - 1)] == undefined) {
colors_sorted_arr[(j - 1)] = [];
colors_sorted_arr[(j - 1)].push(data[i].data[0]);
} else {
colors_sorted_arr[(j - 1)].push(data[i].data[0]);
}
}
}
}

//Now push index1 data to final array
dates_sorted_arr[0] = index1;

//For other data to final array loop through dates array , then inside that loop through all colors
//and then for all colors loop inside each value to check for particular data
for (var i = 0; i < dates.length; i++) {
dates_sorted_arr[(i + 1)] = [];
dates_sorted_arr[(i + 1)].push(dates[i]);
for (var j = 0; j < colors_sorted_arr.length; j++) {
for (var k = 0; k < colors_sorted_arr[j].length; k++) {
if (colors_sorted_arr[j][k].date == dates[i]) {
dates_sorted_arr[(i + 1)].push(colors_sorted_arr[j][k].count);
}
}
}
for (var l = 0; l < index1.length; l++) {
if (dates_sorted_arr[(i + 1)].length != index1.length) {
dates_sorted_arr[(i + 1)].push(0);
}
}
}

//After creating above got final result what you want
console.log(dates_sorted_arr);

关于javascript - 按键分组对象数组并将分组结果与日期数组进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52676569/

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