JavaScript - 比较具有相同字符串的两个数组

时间:2017-05-04 08:41:35

标签: javascript arrays list compare

我目前正在一个项目中工作,我需要比较这两个数组,并过滤​​出具有相同房间名称的数组;

(例如; A420.2 - 0h 53 m(来自空缺 - 阵列)和 A420.2 (来自预订 -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];

过滤后的结果应如下所示;

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

我尝试了几种不同的方法,我从类似的问题中找到了这些方法,但我没有得到我想要的结果。例如;

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

感谢所有的答案,它确实帮助了很多,我的代码工作正常。 无论如何,我有一个跟进问题;

如果过滤后的数组有两个相同的名称,例如;

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

所以我们这里有 FRAMIA250.1 - 0h 34 m FRAMIA250.1 - 3h 34 m 。过滤掉具有相同名称的第二个(FRAMIA250.1 - 3h 34 m)的最有效方法是什么时候从第一个过期(FRAMIA250.1 - 0h 34 m)?

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

6 个答案:

答案 0 :(得分:4)

使用Array#filter()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);

答案 1 :(得分:1)

使用filterincludes,如下所示:

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

答案 2 :(得分:0)

我首先要创建一个ES6 Set以便更快地查找,并将其用作this进行过滤回调:

&#13;
&#13;
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果基本上你想要过滤掉那些也在预订中的那些,如果我做对了:

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

答案 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)

我会写:

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)

使用此检查过滤 vacant 的每个元素 v :如果< - 中的第一个子字符串(split(...)[0])在<在预订的数组中找不到em> v (!includes(...)),保留它。

请参阅includessplitfilterlambda