javascript - 如何在node js中同时对日期和各个日期的值数组进行排序?

标签 javascript node.js mongodb express mongoose

我有一个用例,我将获得多个变量每天的平均数据。日期和值以未排序的方式在 2 个数组中返回。我需要按升序对日期以及值数组中的排序值进行排序。根据日期,现在得到的值是正确的。但我无法正确对两个数组进行排序。即,如果我尝试对日期数组进行排序,则无法获得特定日期的值。有什么办法可以解决这个问题吗?

代码片段是,


let avgValue = [ { _id: { year: 2019, month: 12, day: 11 }, value: 19.83 },
                  { _id: { year: 2019, month: 12, day: 10 }, value: 17.88 },
                  { _id: { year: 2019, month: 12, day: 9 }, value: 32.58 },
                  { _id: { year: 2019, month: 12, day: 6 }, value: 1 },
                  { _id: { year: 2019, month: 12, day: 5 }, value: 33.82 } ];

                [ { _id: { year: 2019, month: 12, day: 11 }, value: 26 },
                  { _id: { year: 2019, month: 12, day: 10 }, value: 40.38 },
                  { _id: { year: 2019, month: 12, day: 9 }, value: 35.2 },
                  { _id: { year: 2019, month: 12, day: 6 }, value: 2 },
                  { _id: { year: 2019, month: 12, day: 5 }, value: 38.91 } ];

**avgValue is a mongodb query which returns the above array for every variable**

let dates = [ '11/12/2019','10/12/2019','9/12/2019','8/12/2019',
              '7/12/2019','6/12/2019','5/12/2019','4/12/2019'];

let dbValues=[],dbDates=[],concatenatedDate,newdates=[],constArray=[];

let avgDates = avgValue.map((date,i)=>(date._id.year&&date._id.month&&date._id.day)?
                                 constArray.push(date._id.day+"/"+date._id.month+"/"+date._id.year):null);

avgValue.forEach(async data=>{
    concatenatedDate = data._id.day+"/"+data._id.month+"/"+data._id.year;
    dates.map((newDate,j)=>{
       if(!constArray.includes(newDate) && !newdates.includes(newDate) && i==j){
              dbValues.push(0);
              newdates.push(newDate);
       }else if(newDate===concatenatedDate ){
              dbValues.push(data.value);
       }
  });
  dbDates.push(newdates);
}

预期输出是,

:[0,77,1,0,0,200,15,22],

标签:[2019年4月12日,2019年5月12日, 2019 年 6 月 12 日, 2019 年 7 月 12 日, 2019 年 8 月 12 日, 2019 年 9 月 12 日, 2019 年 10 月 12 日, 2019年11月12日]

enter image description here

最佳答案

这不是最快的方法,但如果您不想实现排序算法,您可以合并、排序和取消合并数组。

const values = [22,0,0,15,200,1,77]
const labels = ["11/12/2019","9/12/2019","7/12/2019","10/12/2019","9/12/2019","6/12/2019","5/12/2019"];

// merge
const merged = [];
for (let i = 0; i < values.length; ++i) {
    merged.push([values[i], labels[i]]);
}

// sort
merged.sort((a,b) => new  Date(a[1]).getTime() - new Date(b[1]).getTime());

// unmerge
const resValues = [];
const resLabels = [];
for (const mergedValue of merged) {
    resValues.push(mergedValue[0]);
    resLabels.push(mergedValue[1]);
}

// log
// resValues: [ 77, 1, 0, 0, 200, 15, 22 ]
// resLabels: [ '5/12/2019',
//   '6/12/2019',
//   '7/12/2019',
//   '9/12/2019',
//   '9/12/2019',
//   '10/12/2019',
//   '11/12/2019',
// ]

关于javascript - 如何在node js中同时对日期和各个日期的值数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59284640/

相关文章:

Javascript 单击切换开始/恢复失败。clearInterval 不起作用

javascript - 如何使用 Webpack 在 JavaScript 中导入 Frontmatter 和 Markdown

javascript - Node.js 中的异步递归问题

MongoDB 4.2.5 接收来自客户端的请求时出错 : SSLHandshakeFailed: The server is configured to only allow SSL connections

javascript - scrollTop() 并不总是有效

javascript - 如何在没有代码 "editable = true"的情况下调整完整日历中的事件大小

mysql - 使用 zongji 模块监听 mysql 更改事件不起作用

javascript - 为什么settimeout会破坏Expressjs

java - MongoDB 身份验证失败

node.js - 如何使用 node.js express backbone.js mongodb 登录用户?