2

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.

3
  • 1
    These are not valid arrays Commented May 4, 2017 at 8:47
  • @Weedoze I've pushed these elements inside of these arrays, booked.push(resource.code); Commented May 4, 2017 at 8:50
  • Do you need the <br/> in your array? That is additional overhead for calculating similarities. Commented May 4, 2017 at 8:51

6 Answers 6

4

Using Array#filter() and Array#find()

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

var filtered = vacant.filter(v=>!booked.find(b=>b===v.split('-')[0].trim()));
console.log(filtered);

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

2 Comments

Thank you this worked great. Can you also check my follow-up question if possible?
@Destiny_Coder_88 Your follow-up question is totally different. Please close this question by accepting my answer then create another question on SO
1

Use filter and includes like below:

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'];


var ans = vacant.filter(function (v,i) {
  var toSearch = v.split('-')[0].trim();
  return !booked.includes(toSearch);
});

console.log(ans);

5 Comments

booked.includes(toSearch) ? false : true;...C'mon don't be that guy
@Weedoze Sorry, didn't get you?
Use ! to return the opposite instead of using a ternary operator !booked.includes(toSearch) - It is like coding if(value===true)
@PankajShukla Thank you for your answer. Can you also check my follow-up question if possible?
@Destiny_Coder_88 Sorry, but Your follow up question is not clear to me.
0

I would first create an ES6 Set for faster lookup, and use that as this for the filter callback:

const 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"],
      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"]
      filtered = vacant.filter(function (v) {
          return !this.has(v.split('-')[0].trim())
      }, new Set(booked));
console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

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

var filtered = [];

for(var i=0;i<vacant.length;i++){
 var found = false;
 for(var x=0;x<booked.length;x++){
  if(vacant[i].indexOf(booked[x]) > -1){
    found = true;
  }
 }
if(!found){
    filtered.push(vacant[i]);
}
}
var result="";
for(var y=0;y<filtered.length;y++){
 result += filtered[y] + "\n<BR>";
}
 document.getElementById("demo").innerHTML = result;
}

Comments

0

I would write:

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

var filtered = vacant.filter(v => !booked.includes(v.split(" -")[0]))
console.log(filtered)

You filter every element v of vacant using this check: If the first substring before any " -" (split(...)[0]) in v is not found (!includes(...)) in the booked array, keep it.

See includes, split, filter and lambda.

Comments

0

if basically you want to just filter out those from vacant which are also in booking, if I got it right:

function filterVacancies(vacant, booked) {
  return vacant.filter(function(vacancy){
    // now let's search in booked if some element "starts with" the room number
    return booked.some(function(booking){
      return vacancy.startsWith(booking);
    });
  })
}

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.