I'm currently working on a code where I need to compare two arrays and remove multiple elements with the same name. Here are the arrays;
vacant = [
"FRAMIA420.2 - 0h 36 m",
"FRAMIA510.4 - 0h 36 m",
"FRAMIA320.7 - 0h 36 m",
"FRAMIA520.7 - 0h 36 m",
"FRAMIA450.3 - 1h 36 m",
"FRAMIA350.1 - 2h 21 m",
"FRAMIA210.2 - 2h 21 m",
"FRAMIA340.2 - 2h 36 m"]
booked = [
"FRAMIA440.5 - 13h 0 m",
"FRAMIA540.2 - 3h 45 m",
"FRAMIA340.2 - 5h 45 m",
"FRAMIA250.1 - 3h 45 m",
"FRAMIA420.2 - 3h 45 m",
"FRAMIA540.1 - 13h 0 m",
"FRAMIA520.5 - 3h 45 m",
"FRAMIA240.4 - 3h 45 m",
"FRAMIA510.2 - 7h 0 m",
"FRAMIA510.4 - 2h 45 m",
"FRAMIA520.7 - 2h 45 m",
"FRAMIA450.1 - 1h 45 m",
"FRAMIA450.3 - 2h 0 m"]
So the similar elements between these two arrays are: FRAMIA420.2, FRAMIA510.4, FRAMIA520.7, FRAMIA450.3 and FRAMIA340.2
I've already filtered out the timestamp part of an element, so I would only need to compare the name parts;
var firstPart = [];
vacant.forEach(function (obj1) {
firstPart.push(obj1.substring(0, obj1.indexOf('-')))
});
booked.forEach(function (obj2) {
var c = firstPart.indexOf(obj2.substring(0, obj2.indexOf('-')));
});
The final result should look like this, only leaving the elements inside the vacant -array, that had no similarities with the booked -array:
FRAMIA320.7 - 0h 36 m
FRAMIA350.1 - 2h 21 m
FRAMIA210.2 - 2h 21 m
Note that the similarities between the arrays varies everyday, sometimes there might be 2 similar elements and other days there might be 8 or more.
Any quick and effective way to do this?