如何在JavaScript / Jquery中对另一个数组中已经存在的数组元素进行切片[重复]

Posted

技术标签:

【中文标题】如何在JavaScript / Jquery中对另一个数组中已经存在的数组元素进行切片[重复]【英文标题】:How to Slice array Elements that is already present in another array in JavaScript/Jquery [duplicate] 【发布时间】:2016-10-27 00:52:56 【问题描述】:

我有 2 个时间字符串数组。我想删除 arr2 中已经存在的 arr1 元素。

这些都是我的:

 arr1 = ["05:30", "05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45", "21:00", "21:15", "21:30", "21:45", "22:00", "22:15", "22:30", "22:45", "23:00", "23:15", "23:30"]

    arr2=["05:30", "05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "13:00", "22:00", "22:15"]

请帮忙。提前致谢

【问题讨论】:

数据排序了吗? 数据目前已排序,但这对我来说不是必需的或重要的。 这不过是两个数组的联合...见***.com/questions/3629817/… 无法回答,因为它已经是重复的,但您可以使用 grepinArray。 jsfiddle.net/wvmpha59 【参考方案1】:

您可以使用由第二个数组组成的哈希表对其进行过滤。

var arr1 = ["05:30", "05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45", "21:00", "21:15", "21:30", "21:45", "22:00", "22:15", "22:30", "22:45", "23:00", "23:15", "23:30"],
    arr2 = ["05:30", "05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "13:00", "22:00", "22:15"],
    hash = ;

arr2.forEach(function (a) 
    hash[a] = true;
);

arr1 = arr1.filter(function (a) 
    return !hash[a];
);

console.log(arr1);

排序数据的建议。

var arr1 = ["05:30", "05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45", "21:00", "21:15", "21:30", "21:45", "22:00", "22:15", "22:30", "22:45", "23:00", "23:15", "23:30"],
    arr2 = ["05:30", "05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "13:00", "22:00", "22:15"],
    j = 0;

arr1 = arr1.filter(function (a) 
    while (arr2[j] < a) 
        j++;
    
    return arr2[j] !== a;
);

console.log(arr1);

【讨论】:

感谢您的回答和 sn-p 示例:)【参考方案2】:

你可以先在arr2上创建hash map

var lookupMap = ;

arr2.forEach(function(value)
lookupMap[value] = true;
);

arr1 = arr1.filter(function(value) 
return !lookupMap[value]
);

【讨论】:

感谢您的帮助 ;)

以上是关于如何在JavaScript / Jquery中对另一个数组中已经存在的数组元素进行切片[重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Excel 中对另一个工作簿进行相对引用?

如何在java中对另一个选择查询的结果集执行选择查询

SQLite如何从分隔列文本中对另一个表中的值求和?

在javascript / jQuery中对关联数组执行“Diff”操作?

使用 Perl 中对列表进行排序的索引对另一个列表进行排序和索引

如何在每个循环中对 Json 对象进行分组