用相同的字符串比较两个数组

时间:2022-04-24 12:19:01

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)).

(例如;A420.2 - 0h53 m(空-数组)和A420.2(从预定-数组)。

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)?

所以我们这里有FRAMIA250.1 - 0h34 m和FRAMIA250.1 - 3h34 m。从第一个(ia250.1 - 0h34 m)到时间过期,过滤第二个同名的(FRAMIA250.1 - 3h34 m)最有效的方法是什么?

TO CLARIFY; When the time expires it no longer shows the element in the filtered array.

澄清;当时间过期时,它不再显示过滤数组中的元素。

6 个解决方案

#1


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);

#2


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);

#3


0  

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

我将首先创建一个ES6集用于更快的查找,并将其用于过滤器回调:

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; }

#4


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;
}

#5


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.

您可以使用这个检查来过滤每个空元素v:如果在v中的任何“-”(split(…)[0])之前的第一个子字符串没有在已预订的数组中找到(!include(…),请保存它。

See includes, split, filter and lambda.

参见包含、分割、筛选和lambda。

#6


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);
    });
  })
}

#1


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);

#2


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);

#3


0  

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

我将首先创建一个ES6集用于更快的查找,并将其用于过滤器回调:

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; }

#4


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;
}

#5


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.

您可以使用这个检查来过滤每个空元素v:如果在v中的任何“-”(split(…)[0])之前的第一个子字符串没有在已预订的数组中找到(!include(…),请保存它。

See includes, split, filter and lambda.

参见包含、分割、筛选和lambda。

#6


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);
    });
  })
}