I am trying to try the below code but maybe the timing is wrong
var nhanvien = [{
id1: 001,
id2: 002,
id3:004,
sanluong: 200,
"Start": "5:35:00",
},
{
id1: 003,
id2: 002,
sanluong: 200,
"Start": "8:35:00",
"Start2": "17:16:00",
}, {
id1: 004,
id2: 001,
sanluong: 500,
"Start": "8:35:00",
"Start2": "17:16:00",
"Start3": "17:16:00",
}
]
var a = nhanvien.filter(function (o) { if ( o.id1 == 001 || o.id2 == 001 || o.id3 == 001 ) if(o.Start < "6:00:00") return o })
i just need any object have id = 001 ( id1 or id2 or id3 have value = 001) and start before 5:00:00 AM but it's just right when condition is start, properti start2 is wrong
pecifically, when you run this code, the object return value start2 is bigger than "6:00:00"
var a = nhanvien.filter(function (o) { if ( o.id1 == 001 || o.id2 == 001 || o.id3 == 001 ) if(o.Start2 < "6:00:00") return o })
initially, i used function fillter of lodash then i used function like this code
CodePudding user response:
So, let's elaborate the function that you are using:
var a = nhanvien.filter(function (o) {
if (o.id1 == 001 || o.id2 == 001 || o.id3 == 001)
if(o.Start < "6:00:00")
return o;
})
The issues are following:
- Only returning for one condition. You need to return booleanish value for filtering. So, you can try following:
nhanvien.filter(function (o) {
return (o.id1 == 001 || o.id2 == 001 || o.id3 == 001) && o.Start < "06:00:00"
})
- You can't compare duration using string like that. E.g.-
"17:16:00" > "8:35:00" // returns false
But in your case it should be true. So, you can append zeros.
17:16:00" > "08:35:00" // returns true
Also, for your note, 001 and 1 is same in js unless you convert to string.
CodePudding user response:
The problem is because you are comparing durations using string.
For better handling time string comparison. You can write a function to convert a given time string into seconds:
const toSeconds = timeString => {
const bits = timeString.split(':');
return bits[0] * 3600 bits[1] * 60 bits[2] * 1;
};
And then modify the filter logic like this:
const results = nhanvien.filter(function (o) {
if (o.id1 === '001' || o.id2 === '001' || o.id3 === '001') {
if (toSeconds(o.Start) < toSeconds('06:00:00')) {
return true;
}
}
return false;
});
CodePudding user response:
Refer string-comparison to know how <
& >
works with string
. Instead of comparing the string
, I would prefer to convert your time value to seconds
and then compare.
var nhanvien = [{
id1: 001,
id2: 002,
id3: 004,
sanluong: 200,
"Start": "5:35:00",
}, {
id1: 003,
id2: 002,
sanluong: 200,
"Start": "8:35:00",
"Start2": "17:16:00",
}, {
id1: 004,
id2: 001,
sanluong: 500,
"Start": "8:35:00",
"Start2": "17:16:00",
"Start3": "17:16:00",
}];
// get seconds from time string
function getSeconds(str) {
// split time string with : and convert values to integer
let parts = str.split(':').map(i => parseInt(i));
// multiply hour with 3600, minutes with 60 & add them with seconds
return parts[0] * 3600 parts[1] * 60 parts[2];
}
// compare two string
function compareTime(time1, time2) {
// check if we have values in time1 & time2 variables
// if both variables having values then check both are having valid time value, ie. it has hour, minute & seconds separated by :
// compare both times by converting them to seconds
return time1 && time2 && time1.split(':').length == 3 && time2.split(':').length == 3 && getSeconds(time1) < getSeconds(time2);
}
// use arrow function in filter and we need to return boolean expression
var a = nhanvien.filter(o => (o.id1 == 001 || o.id2 == 001 || o.id3 == 001) && compareTime(o.Start, "6:00:00"));
var b = nhanvien.filter(o => (o.id1 == 001 || o.id2 == 001 || o.id3 == 001) && compareTime(o.Start2, "18:00:00"));
console.log("a", a);
console.log("b", b);