问题
On a follow up to my question. I am trying to sort my array in different ways.
I first wanted to add the missing values in my array of objects and then wanted to be able to sort it. Since my data is dynamic, I created a function and then called it based on how I wanted it sorted.
I am not getting any errors but I am still not able to do the sorting.
My code:
var json = [{
"date": "2020-01-01T00:00:00.000Z",
"metric": 32,
"type": "Google"
},
{
"date": "2020-01-01T00:00:00.000Z",
"metric": 24,
"type": "Bing"
},
{
"date": "2020-02-01T00:00:00.000Z",
"metric": 1,
"type": "Google"
},
{
"date": "2020-02-01T00:00:00.000Z",
"metric": 32,
"type": "Jeeves"
},
{
"date": "2020-03-01T00:00:00.000Z",
"metric": 24,
"type": "Bing"
},
{
"date": "2020-03-01T00:00:00.000Z",
"metric": 30,
"type": "Google"
}
];
const the_series = (cat, data, group, metrics) => {
let cat_arr = [];
vals = data.map(val => {
return val[cat];
});
vals.forEach(v=>{
if (cat_arr.indexOf(v) === -1) cat_arr.push(v);
});
console.log(cat_arr);
const gp = (array, key) => {
return array.reduce((a, c) => {
(a[c[key]] = a[c[key]] || []).push(c);
return a;
}, {});
}
const get_gp = gp(data, group);
for (a in get_gp) {
cat_arr.forEach(t => {
if (!get_gp[a].some(v => v[cat] == t)) {
let f_gp = get_gp[a][0][group];
let f_cats = t;
console.log(group + ' -- ' + cat);
let inc = {};
inc[group] = f_gp;
inc[cat] = f_cats;
inc[metrics] = 0;
get_gp[a].push(inc);
}
});
get_gp[a].sort((a, b) => cat_arr.indexOf(a[cat])-cat.indexOf(b[cat]));
}
console.log(get_gp);
let series = Object.entries(get_gp).map(([key, group]) => ({
['name']: key,
['data']: group.map(entry => entry[metrics]),
['marker']: {
symbol: 'circle'
}
}));
console.log(series);
}
console.log('-------------------------STRINGS--------------------------');
the_series('type', json, 'date', 'metric');
console.log('-------------------------DATES--------------------------');
the_series('date', json, 'type', 'metric');
I have created a fiddle.
Expected Result:
2020-01-01: Array(3)
0: {date: "2020-01-01", metric: 32, type: "Google"}
1: {date: "2020-01-01", metric: 24, type: "Bing"}
2: {date: "2020-01-01", metric: 0, type: "Jeeves"}
length: 3
__proto__: Array(0)
2020-01-02: Array(3)
0: {date: "2020-01-02", metric: 1, type: "Google"}
1: {date: "2020-01-02", metric: 0, type: "Bing"}
2: {date: "2020-01-02", metric: 32, type: "Jeeves"}
length: 3
__proto__: Array(0)
2020-01-03: Array(3)
0: {date: "2020-01-03", metric: 30, type: "Google"}
1: {date: "2020-01-03", metric: 24, type: "Bing"}
2: {date: "2020-01-03", metric: 0, type: "Jeeves"}
Bing: Array(3)
0: {date: "2020-01-01T00:00:00.000Z", metric: 24, type: "Bing"}
1: {type: "Bing", date: "2020-02-01T00:00:00.000Z", metric: 0}
2: {date: "2020-03-01T00:00:00.000Z", metric: 24, type: "Bing"}
length: 3
__proto__: Array(0)
Google: Array(3)
0: {date: "2020-01-01T00:00:00.000Z", metric: 32, type: "Google"}
1: {date: "2020-02-01T00:00:00.000Z", metric: 1, type: "Google"}
2: {date: "2020-03-01T00:00:00.000Z", metric: 30, type: "Google"}
length: 3
__proto__: Array(0)
Jeeves: Array(3)
0: {type: "Jeeves", date: "2020-01-01T00:00:00.000Z", metric: 0}
1: {date: "2020-02-01T00:00:00.000Z", metric: 32, type: "Jeeves"}
2: {type: "Jeeves", date: "2020-03-01T00:00:00.000Z", metric: 0}
来源:https://stackoverflow.com/questions/61670437/not-able-to-sort-array-of-objects-after-adding-values