[英]convert a property and return the array of object using javascript
I have array of object, in which property time我有 object 的数组,其中属性时间
is in format dddd hh:min
and need to convert to minutes, return the array object.格式为
dddd hh:min
,需要转换为分钟,返回数组object。
Current format dddd: days(in 4 digits) hh: hour(in 2 digits) min: (2 digits)当前格式 dddd: days(in 4 digits) hh: hour(in 2 digits) min: (2 digits)
I tried below code, works but return array of object is wrong.我试过下面的代码,有效但 object 的返回数组是错误的。
So, I need to know how to convert the time property to minutes, and return the所以,我需要知道如何将时间属性转换为分钟,并返回
arrayobject using javascript. Better approach also can arrayobject 使用 javascript。更好的方法也可以
var arrobj=[
{id:1, time: '0001 03:40'},
{id:2, time: '0016 10:20'},
{id:3, time: '1014 12:04'},
{id:4, time: '0412 01:01'},
]
function convertToMinutes(value) {
var splitTime = value.split(' ');
var dayToMinutes = splitTime[0] * 1440;
console.log(dayToMinutes);
var splitHrMin = splitTime[1].split(':');
console.log(splitHrMin);
var splitHr = splitHrMin[0] * 60;
console.log(splitHr);
var splitMin = splitHrMin[1];
var timeInMinutes = dayToMinutes + splitHr + splitMin;
console.log(timeInMinutes);
return timeInMinutes;
}
for(let item of arrobj) {
var result = this.convertToMinutes(item.time);
console.log(result);
}
Expected Output
[
{id:1, time: '1660'},
{id:2, time: '23660'},
{id:3, time: '1460884'},
{id:4, time: '593341'},
]
This should work for what you are trying to achieve这应该适用于您要实现的目标
var arrobj=[ {id:1, time: '0001 03:40'}, {id:2, time: '0016 10:20'}, {id:3, time: '1014 12:04'}, {id:4, time: '0412 01:01'}, ] const convertToMinutes = (obj) => { const newObj = []; obj.forEach(val => { var day = val.time.split(" "); var [hr, min] = day[1].split(":"); var time = (((+day[0] * 24) + +hr) * 60) + +min; newObj.push({id: val.id, time: `${time}`}) }); return newObj; }; console.log(convertToMinutes(arrobj));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.