1

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?

4 Answers 4

2

You can construct a list of all names in the booked array, then iterate through the vacant array, checking for whether that name is in the list of booked names.

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"
]

function getName(str) {
  return str.substring(0, str.indexOf('-'));
}

var bookedNames = [];
booked.forEach(function (bookedStr) {
  bookedNames.push(getName(bookedStr))
});

var uniqueVacant = [];
vacant.forEach(function (vacantStr) {
  var vacantName = getName(vacantStr);
  if (!bookedNames.includes(vacantName))
    uniqueVacant.push(vacantStr)
});
console.log(uniqueVacant);

Sign up to request clarification or add additional context in comments.

Comments

1

You need 2 loops, one for each array, and compare the first part of the string of each array, like so:

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"]

for(i=0;i<vacant.length;i++) {
    item1 = vacant[i].split('-')[0];
    for(j=0;j<booked.length;j++) {
        item2 = booked[j].split('-')[0];
        if(item1===item2) {
                console.log('item number '+i+' in vacant is the same as item number '+j+' in booked');
        }
    }
}

https://jsfiddle.net/48hef0cz/

Comments

1

Try this :

var 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"];

var 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"];

vacant = vacant.filter(function (element) {
  var roomName = element.split('-')[0];
  
  var index = booked.findIndex(function (booking) {
    return roomName === booking.split('-')[0];
  });
  
  return index == -1;
});

console.log(vacant);

Comments

1

You can use a dictionary to keep track of unique values.

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"]

vacantDict = {};

vacant.forEach(function(val) {
  var name = val.split(' - ')[0];
  vacantDict[name] = val;
});

booked.forEach(function(val) {
  var name = val.split(' - ')[0];
  if (vacantDict[name] !== undefined) {
    delete vacantDict[name];
  }
});

newVacantList = [];
for (var name in vacantDict) {
  newVacantList.push(vacantDict[name]);
}

console.log(newVacantList);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.