简体   繁体   English

以 YYYY-MM-DD 格式从数组中获取上个月的所有记录

[英]Get all records from an array for the last month in YYYY-MM-DD format

How can I find all objects in array [{value:"String", date:"YYYY-MM-DD"}] with the last provided month and year of course?Year range is 2018-2020.我如何才能找到数组[{value:"String", date:"YYYY-MM-DD"}]所有对象,当然还有最后提供的月份和年份?年份范围是 2018-2020。 I want something instead of slicing and equaling.我想要一些东西而不是切片和相等。

For example, i got: 50 items array with value and date property.例如,我得到: 50 个具有值和日期属性的数组。 Value is simple price in string format and date is like "2020-02-12", "2019-12-13", "2019-04-28" and so on.值是字符串格式的简单价格,日期如“2020-02-12”、“2019-12-13”、“2019-04-28”等。 So I need to get all values from the last month and sum them.So the problem is in the finding last month.所以我需要获取上个月的所有值并将它们相加。所以问题出在上个月的发现中。 Array is dynamic, that means that I got like 300 variation of the array, as a result last month and year each time could be different.数组是动态的,这意味着我得到了大约 300 个数组的变化,因此上个月和上年的每次都可能不同。

As I mentioned in the comment section, you can convert each strings which has date values with new Date() then using .filter() on the array would do the job.正如我在评论部分提到的,您可以使用new Date()转换每个具有日期值的字符串,然后在数组上使用.filter()就可以完成这项工作。

See the following example:请参阅以下示例:

 const data = [{value: 'val3', date: '2017-05-06'},{value: 'val2', date: '2018-05-06'},{value: 'val1', date: '1988-05-06'},{value: 'val4', date: '2019-03-22'},{value: 'val6', date: '2019-12-24'}]; const result = data.filter(e => new Date('2018-01-01') < new Date(e.date) && new Date(e.date) < new Date('2020-01-01') ); console.log(result);

I hope this helps!我希望这有帮助!

as I understand it, you need the sum of all the values for the last month.据我了解,您需要上个月所有值的总和。 I think the code below is what you need:我认为下面的代码是你需要的:

 let data = [{value: '43.21', date: '2019-07-06'},{value: '24.34', date: '2017-03-03'},{value: '54.32', date: '2020-02-03'},{value: '11.32', date: '2020-02-05'},{value: '34.23', date: '2019-11-21'},{value: '32.34', date: '2020-01-24'},{value: '32.34', date: '2020-02-21'}]; //find last newest record to get it's date let lastdate=new Date(data.reduce(function (a, b) { return a.date > b.date ? a : b; }).date); let result = data.reduce(function(sum, e){ return (isNaN(parseFloat(sum))?0:parseFloat(sum))+(new Date(lastdate.getFullYear(), lastdate.getMonth(), 1) < new Date(e.date) && new Date(e.date) <= new Date(lastdate.getFullYear(), lastdate.getMonth() + 1, 0)?parseFloat(e.value):0) }); document.write("last year/month: " + lastdate.getFullYear()+"/"+(lastdate.getMonth()+1)); document.write("<br>result: "+ result);

Given the a date format of YYYY-MM-DD, the values will sort and compare lexically.给定 YYYY-MM-DD 的日期格式,这些值将按词法排序和比较。 So first find the last month, then filter for dates in that month, eg所以首先找到上个月,然后过滤该月的日期,例如

 let data = [ {a:'a', date:'2020-02-12'}, {a:'a', date:'2019-12-13'}, {a:'a', date:'2019-04-28'}, {a:'a', date:'2019-12-15'}, {a:'a', date:'2020-02-28'}, {a:'a', date:'2018-12-13'}]; // Get last date let last = data.map(obj => obj.date).sort()[data.length-1]; console.log(last); // Get objects in last month let lastMonthsValues = data.filter( obj => obj.date.substr(0,7) == last.substr(0,7) ); // Last month's objects console.log(lastMonthsValues);

You could do the same thing using Date objects, but that requires firstly parsing to Date, then comparing using date.getUTCFullYear() and date.getUTCMonth() instead of simple string operations.您可以使用 Date 对象做同样的事情,但这需要首先解析为 Date,然后使用date.getUTCFullYear()date.getUTCMonth()而不是简单的字符串操作进行比较。

 let data = [ {a:'a', date:'2020-02-12'}, {a:'a', date:'2019-12-13'}, {a:'a', date:'2019-04-28'}, {a:'a', date:'2019-12-15'}, {a:'a', date:'2020-02-28'}, {a:'a', date:'2018-12-13'}]; // Get last date let last = data.map(obj => obj.date).sort((a, b) => new Date(a) - new Date(b))[data.length-1]; console.log(last); // Get objects in last month let lastMonthsValues = data.filter(obj => { let d0 = new Date(last); let d1 = new Date(obj.date); return d1.getUTCFullYear() == d0.getUTCFullYear() && d1.getUTCMonth() == d0.getUTCMonth(); } ); // Last month's objects console.log(lastMonthsValues);

Using string methods is much less code and likely very much more efficient.使用字符串方法的代码要少得多,而且效率可能要高得多。

I did myself something like this:我自己做了这样的事情:

info.incomes.sort(function(a, b) {
    return new Date(b.date)-new Date(a.date);
});
let comparison =[];
for (let i in info.incomes){    
comparison.push(info.incomes[i].date.substr(0, 7));
}
let lastmonth = 0;
for (let i in comparison){
    if (comparison[0] === comparison[i]){        
        lastmonth+=parseFloat(info.incomes[i].value);
    }      
}

I just need last month incomes to be summarized, so i just sorted them, and checked if they equals to my last date.我只需要总结上个月的收入,所以我只是对它们进行了排序,并检查它们是否等于我的最后日期。 Sorry for bad variables naming =)抱歉变量命名错误=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将“ yyyy-mm-dd”日期格式设置为“ dd month yyyy” - Format a “yyyy-mm-dd” date into “dd month yyyy” 如何使用应用程序脚本以“yyyy-mm-dd”的格式获取当前周和月的第一个日期和最后一个日期 - How to get the 1st date and last date of the current Week as well as Month in the format of 'yyyy-mm-dd' using App Scripts 从 yyyy-mm-dd 获取时间戳 - Get timestamp from yyyy-mm-dd 如何将对象数组中格式为 YYYY-MM-DD 的所有字符串转换为新的 Date(YYYY, MM, DD).getTime() - How to convert all strings in format YYYY-MM-DD in an array of objects to new Date(YYYY, MM, DD).getTime() 如何从日期格式YYYY-MM-DD检查两个月是否相等? - How to check two month are equal or not from date format YYYY-MM-DD? 将日期格式从“ yyyy-mm-dd”更改为“ yyyy MMM dd”或“ MMM dd,yyyy” - Changing date format from 'yyyy-mm-dd' to 'yyyy MMM dd' or 'MMM dd, yyyy' 输入后将日期格式从 mm/dd/yyyy 转换为 yyyy-mm-dd 格式 - Converting date format from mm/dd/yyyy to yyyy-mm-dd format after entered 将dd.mm.yyyy格式转换为yyyy-mm-dd - Convert dd.mm.yyyy format to yyyy-mm-dd 如何获取 YYYY-MM-DD 格式的日期? - How do I get a date in YYYY-MM-DD format? 将日期格式(yyyy-mm-dd)从数据库更改为dd / mm / yyyy - Change of date format (yyyy-mm-dd) from DB into dd/mm/yyyy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM