JavaScript-比较两个具有相同字符串的数组 [英] JavaScript - Comparing two arrays with same strings

查看:91
本文介绍了JavaScript-比较两个具有相同字符串的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently working in a project, where I need to compare these two arrays and filter out the ones with same room name;

(例如, A420.2 -0h 53 m(来自空缺-阵列)和 A420.2 (来自已预订-数组)).

(for example; A420.2 - 0h 53 m (from vacant -array) and A420.2 (from booked -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];

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

The filtered result should look like the following;

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

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

I've tried couple of different methods, that I've found from similar questions, but I didn't get the result I was looking for. for example;

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

感谢所有答案,这确实有很大帮助,我的代码也能正常工作. 无论如何,我有一个跟您问的后续问题;

Thanks for all the answers, it really helped a lot and I got my code working. Anyhow, I have a follow-up question for you;

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

If the filtered array has two of the same name, for example;

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)到期?

So we have here FRAMIA250.1 - 0h 34 m and FRAMIA250.1 - 3h 34 m. What is the most efficient way to filter out the second one with the same name (FRAMIA250.1 - 3h 34 m) UNTIL the time expires from the first one (FRAMIA250.1 - 0h 34 m)?

澄清;时间到时,它将不再显示已过滤数组中的元素.

TO CLARIFY; When the time expires it no longer shows the element in the filtered array.

推荐答案

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

这篇关于JavaScript-比较两个具有相同字符串的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆