I'm currently working in a project, where I need to compare these two arrays and filter out the ones with same room name;
(for example; A420.2 - 0h 53 m (from vacant -array) and A420.2 (from booked -array)).
var vacant = [
A210.3 - 0h 53 m
,A510.2 - 0h 53 m
,A510.4 - 0h 53 m
,A340.2 - 0h 53 m
,A420.2 - 0h 53 m
,A450.1 - 1h 53 m
,A250.1 - 1h 53 m
,A520.7 - 2h 53 m
,A510.2 - 2h 53 m
,A240.2 - 2h 53 m
,A440.2 - 2h 53 m
,A350.1 - 4h 38 m
,A250.1 - 4h 53 m
,A450.3 - 4h 53 m
,A340.1 - 4h 53 m
,A320.6 - 4h 53 m
,A210.2 - 5h 38 m
,A240.2 - 6h 53 m
,A240.4 - 6h 53 m];
var booked = [
A130.1
,A420.6
,A440.5
,A540.1
,A250.1
,A350.1
,A420.2
,A510.2
,A320.6
,A320.7
,A210.2
,A220.3];
The filtered result should look like the following;
var filtered = [
A210.3 - 0h 53 m
,A510.4 - 0h 53 m
,A340.2 - 0h 53 m
,A450.1 - 1h 53 m
,A250.1 - 1h 53 m
,A520.7 - 2h 53 m
,A240.2 - 2h 53 m
,A440.2 - 2h 53 m
,A450.3 - 4h 53 m
,A340.1 - 4h 53 m
,A320.6 - 4h 53 m
,A240.2 - 6h 53 m
,A240.4 - 6h 53 m];
// Filtered out: A250.1, A510.2, A210.2, A420.2, A350.1
I've tried couple of different methods, that I've found from similar questions, but I didn't get the result I was looking for. for example;
function arr_diff (booked, vacant) {
var a = [], diff = [];
for (var i = 0; i < booked.length; i++) {
a[booked[i]] = true;
}
for (var i = 0; i < vacant.length; i++) {
if (a[vacant[i]]) {
delete a[vacant[i]];
} else {
a[vacant[i]] = true;
}
}
for (var k in a) {
diff.push(k);
}
return diff;
};
Thanks for all the answers, it really helped a lot and I got my code working. Anyhow, I have a follow-up question for you;
If the filtered array has two of the same name, for example;
FRAMIA250.1 - 0h 34 m
FRAMIA450.1 - 0h 34 m
FRAMIA240.2 - 1h 34 m
FRAMIA510.2 - 1h 34 m
FRAMIA440.2 - 1h 34 m
FRAMIA520.7 - 1h 34 m
FRAMIA350.1 - 3h 19 m
FRAMIA450.3 - 3h 34 m
FRAMIA340.1 - 3h 34 m
FRAMIA250.1 - 3h 34 m
FRAMIA320.6 - 3h 34 m
FRAMIA210.2 - 4h 19 m
FRAMIA240.4 - 5h 34 m
FRAMIA240.2 - 5h 34 m
So we have here FRAMIA250.1 - 0h 34 m and FRAMIA250.1 - 3h 34 m. What is the most efficient way to filter out the second one with the same name (FRAMIA250.1 - 3h 34 m) UNTIL the time expires from the first one (FRAMIA250.1 - 0h 34 m)?
TO CLARIFY; When the time expires it no longer shows the element in the filtered array.
in your array? That is additional overhead for calculating similarities. – Omkar Khair May 04 '17 at 08:51