2017-05-10 50 views
1

我目前正在研究一個代碼,我需要比較兩個數組並刪除具有相同名稱的多個元素。這裏是數組;這兩個陣列之間JavaScript - 刪除兩個數組之間的多個相同的值

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

所以相似的元件是:FRAMIA420.2,FRAMIA510.4,FRAMIA520.7,FRAMIA450.3FRAMIA340.2

我已經過濾了元素的timestamp部分,所以我只需要比較名稱部分;

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

最後的結果應該是這樣的,只留下vacant - 陣列,這同booked -array沒有相似之處裏面的元素:

FRAMIA320.7 - 0h 36 m 
FRAMIA350.1 - 2h 21 m 
FRAMIA210.2 - 2h 21 m 

注意陣列之間的相似性變化每天,有時可能有2個相似的元素,其他日子可能有8個或更多。

任何快速有效的方法來做到這一點?

回答

2

您可以構建的所有名稱的列表booked數組,然後遍歷空數組,檢查名稱是否在預定名稱列表中。

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

+2

謝謝!這很好。 – IlariM

1

你需要2個循環,每個陣列,並比較每個陣列的字符串的第一部分,像這樣:

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/

1

試試這個:

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

1

您可以使用字典來跟蹤唯一值的。

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

相關問題