I need time to compare online and offline time. If is more off-line time than online time - this time I need to delete. When the user has no time line - I need to delete this user.
I have this problem: When going through times and smažuj is: there remains a value of "undefined" - [undefined, undefined] and then this condition can not be satisfied -
if (user.connectionTimes.length === 0) {
delete users[id];
}
How do I solve this problem?
I use the following code:
var users = [];
users[0] = {
userId: 1,
connectionTimes:
[
{onlineTime:30, offlineTime:70},
{onlineTime:50, offlineTime:75}
]
};
users[1] = {
userId: 2,
connectionTimes:
[
{onlineTime:80, offlineTime:70}
]
};
users[2] = {
userId: 3,
connectionTimes: []
};
for( var i=0; i<users.length; i++) {
for( var j=0; j<users[i].connectionTimes.length; j++) {
if( users[i].connectionTimes[j].onlineTime < users[i].connectionTimes[j].offlineTime) {
delete users[i].connectionTimes[j];
//EDIT - this work - users[i].connectionTimes.splice(j, 1);
//j--;
}
}
if( users[i].connectionTimes.length == 0) {
delete users[i];
//EDIT - this work - users.splice(i, 1);
//i--;
}
}
console.log(users);