我正在编写一个代码,我需要比较两个数组并删除多个具有相同名称的元素。这是数组;
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个或更多。
任何快速有效的方法吗?
答案 0 :(得分: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);
答案 1 :(得分: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');
}
}
}
答案 2 :(得分: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);
&#13;
答案 3 :(得分: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);
&#13;