我目前正在处理一个代码,我需要比较两个数组并删除多个同名元素。这是数组;
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.3 和 FRAMIA340.2
。我已经过滤掉了元素的时间戳部分,所以我只需要比较名称部分;
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
-array 内,与booked
-array 没有相似之处:
FRAMIA320.7 - 0h 36 m
FRAMIA350.1 - 2h 21 m
FRAMIA210.2 - 2h 21 m
请注意,数组之间的相似之处每天都在变化,有时可能有 2 个相似的元素,有时可能有 8 个或更多。
有什么快速有效的方法可以做到这一点吗?
您可以构造 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 个循环,每个数组一个,并比较每个数组字符串的第一部分,如下所示:
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/
试试这个:
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);
您可以使用字典来跟踪唯一值。
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);