javascript - How to compare two string arrays, case insensitive and independent about ordering - JS, ES6 - Stack Overflow

I want to pare two string arrays, but case insensitive and independent.For the example:['a',

I want to pare two string arrays, but case insensitive and independent.

For the example:

['a', 'b', 'c'] === ['A', 'c', 'B'] -> TRUE

['a', 'b', 'c'] === ['a', 'b', 'd'] -> FALSE

TRUE when they are with the same length and same values (case insensitive ['A'] === ['a'] -> true) and independent, about ordering ['a', 'b'] === ['b', 'a'] -> true.

What I did for now is:

areEqual = (arr1, arr2) => {
    const equalLength = arr1.length === arr2.length;

    return arr2.every(arr2Item => {

        return arr1.includes(arr2Item.toLowerCase());

    }) && equalLength;
};

, but this is case sensitive.

I am using JS, ES6 with React.

I want to pare two string arrays, but case insensitive and independent.

For the example:

['a', 'b', 'c'] === ['A', 'c', 'B'] -> TRUE

['a', 'b', 'c'] === ['a', 'b', 'd'] -> FALSE

TRUE when they are with the same length and same values (case insensitive ['A'] === ['a'] -> true) and independent, about ordering ['a', 'b'] === ['b', 'a'] -> true.

What I did for now is:

areEqual = (arr1, arr2) => {
    const equalLength = arr1.length === arr2.length;

    return arr2.every(arr2Item => {

        return arr1.includes(arr2Item.toLowerCase());

    }) && equalLength;
};

, but this is case sensitive.

I am using JS, ES6 with React.

Share Improve this question edited Dec 20, 2018 at 19:45 gdfgdfg asked Dec 20, 2018 at 18:49 gdfgdfggdfgdfg 3,5868 gold badges46 silver badges85 bronze badges 7
  • Possible duplicate of How to pare arrays in JavaScript? – Keno Commented Dec 20, 2018 at 18:51
  • Is this correct? ['a', 'b', 'c'] === ['a', 'c'] -> TRUE – Alexander O'Mara Commented Dec 20, 2018 at 18:53
  • 3 How can ['a', 'b', 'c'] === ['a', 'c'] -> TRUE be true if equalLength is false? – Andy Commented Dec 20, 2018 at 18:54
  • 1 What is your expectation about ['a', 'a', 'b'] and ['b', 'b', 'a']? Should they be equal? They have the same length and the same set of values, but each with different multiplicities. If you say they're equal, then shouldn't ['a', 'a', 'a', 'b'] also be equal to them? – Scott Sauyet Commented Dec 20, 2018 at 18:59
  • 3 Your third example still says TRUE for arrays of different lengths. – T.J. Crowder Commented Dec 20, 2018 at 18:59
 |  Show 2 more ments

2 Answers 2

Reset to default 7

You could normalise the strings to lower case and use a Set for checking the values.

function pare(a, b) {
    const lower = s => s.toLowerCase();
    return b
        .map(lower)
        .every(Set.prototype.has, new Set(a.map(lower)));
}

console.log(pare(['a', 'b', 'c'], ['A', 'c', 'B'])); //  true
console.log(pare(['a', 'b', 'c'], ['a', 'b', 'd'])); // false
console.log(pare(['a', 'b', 'c'], ['a', 'c']));      //  true

You need to replace arr1 with a lowercase copy of it. You should also return immediately if they're not the same length, rather than going through all the parison work when it's not necessary.

areEqualCI = (arr1, arr2) => {
    if (arr1.length != arr2.length) {
        return false;
    }
    const arr1Lower = arr1.map(e => e.toLowerCase());
    return arr2.every(arr2Item => {
        return arr1Lower.includes(arr2Item.toLowerCase());
    });
};

It might also be better to sort the two arrays, then just pare them elementwise:

areEqualCI = (arr1, arr2) => {
    if (arr1.length != arr2.length) {
        return false;
    }
    const arr1Lower = arr1.map(e => e.toLowerCase()).sort();
    const arr2Lower = arr2.map(e => e.toLowerCase()).sort();
    for (let i = 0; i < arr1.length; i++) {
        if (arr1[i] != arr2[i]) {
            return false;
        }
    }
    return true;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745650130a4638211.html

相关推荐

  • 流固耦合:基本概念、适用软件及 Abaqus 与 Powerflow 的协同仿真

    在工程和科学研究的诸多领域,流固耦合现象广泛存在且对系统性能有着关键影响。理解流固耦合的本质及其应用,对于优化设计、保障安全运行意义重大。同时,借助专业的流固耦合软件,能够更高效地对相关问题进行分析与模拟。接下来,让我们深入探究流固耦合的奥

    1小时前
    00
  • 文章降 AI 痕迹方法与工具速览

    文章降AI的方法和工具汇总‌如下: 这几天又认真研究类了一下,想让 AI 生成的文章更自然,摆脱程式化痕迹,可尝试以下方法。借助 GPT、文字滚筒鸭,朱雀大模型检测器、豆包、kimi 等大模型,输入文本后,它们能通过调整结构、替换同义词等操

    1小时前
    00
  • MySQL 8.4 配置SSL组复制(八个步骤)

    环境这里有三台MySQL主机,分别是192.168.3.71,72,73,主机名分别对应71.3_mgr1,72.3_mgr2,73.3_mgr3,操作系统均为Oracle Linux 8.10 X64,MySQL版本均为MySQL 8.4

    1小时前
    00
  • MySQL8.4 Enterprise安装Firewall及测试

    参考:.4enfirewall.html1.首先执行安装SQL,路径在baseshare目录下代码语言:javascript代码运行次数:0运行复制cd u01mysql3308baseshare[root@mysql8_3

    1小时前
    00
  • MySQL8.4创建keyring给InnoDB表进行静态数据加密

    参考文档:.4enkeyring-plugin-installation.html.4enkeyring-hashicorp-plugin.html#keyring-hashicorp-vault-configuration1.首先

    1小时前
    10
  • Go项目实战

    上节课我给大家介绍了怎么给Go项目做单元测试的规划,当然这里仅限于跟咱们课程里的实战项目一样分层架构设计做的还可以的项目哦,要是所有逻辑都耦合在Controller里,那这个规划就不适用了。。。,所有逻辑都耦合在Controller里还做个

    1小时前
    00
  • 聊聊Spring AI Alibaba的ObsidianDocumentReader

    序本文主要研究一下Spring AI Alibaba的ObsidianDocumentReaderObsidianDocumentReadercommunitydocument-readersspring-ai-alibaba-star

    1小时前
    00
  • PackML over OPC UA

    在当今数字化转型的浪潮中,制造业正面临着前所未有的挑战与机遇。如何实现设备之间的高效通信与集成,成为提升生产效率、降低成本的关键。OPC UA(OPC Unified Architecture)与PackML(Packaging Machi

    58分钟前
    00
  • 1.8w字图解Java并发容器: CHM、ConcurrentLinkedQueue、7 种阻塞队列的使用场景和原理

    文章多图且内容硬核,建议大家收藏上一章《1.6w 字图解 Java 并发:多线程挑战、线程状态和通信、死锁;AQS、ReentrantLock、Condition 使用和原理》,我们开启了 Java 高并发系列的学习,透彻理解 Java 并

    55分钟前
    00
  • 电脑密码在哪里设置win11,win11电脑开机密码怎么设置

    Win11系统由于许多设置和以前系统不一样了&#xff0c;所以很多用户们操作非常不习惯&#xff0c;有很多的小伙伴不知道win11系统怎么设置开机密码。给电脑设置密码&#xff0c;只有自己能打开进入系统桌面&a

    53分钟前
    00
  • HLS最全知识库

    HLS最全知识库副标题-FPGA高层次综合HLS(二)-Vitis HLS知识库高层次综合(High-level Synthesis)简称HLS,指的是将高层次语言描述的逻辑结构,自动转换成低抽象级语言描述的电路模型的过程。对于AMD Xi

    49分钟前
    00
  • 我用AI监控了奥特曼,当他一发推特AI就会自动给我打电话。

    上周我真的扛不住了。奥特曼这个孙贼,发了个X说,“要发一个礼拜的好东西”。我信了他的邪,明明出差1周,每天早上9点不到就要起来参加活动,但是晚上根本不敢睡觉,天天蹲到凌晨3点半,蹲到他们那边时间中午12点多,我才敢去睡觉。真的,那一整周,我

    48分钟前
    00
  • 20 万 POC,直接拿来用,这不是测试,这是拒绝服务!!!

    之前看到很多人分享 github 上的一个项目,自动收录全网 nuclei 模板文件,目前已经 19 万了,如果直接拿来对目标进行漏洞探测,无疑会对目标造成巨大伤害,意味着可能要对目标发起十九万次请求以上,可以说是一次小型的 DDoS 攻击

    39分钟前
    00
  • ascend pytorch 踩坑.

    在910b上安装pytorch 和 pytorch_npu, 因为后续准备装vllm, 所以torch_npu是特殊的版本.代码语言:shell复制pip install torch==2.5.1 --extra-index pip in

    28分钟前
    00
  • Power BI 无公式实现帕累托图表

    帕累托分析(Pareto Analysis),也被称为8020法则、关键少数法则,是一种常用的管理工具,用于识别和处理影响业务的主要因素。看到李伟坚老师在Excel使用Vega实现了花式帕累托(参考:Excel 零公式实现高级帕累托图表)

    25分钟前
    00
  • 什么是docker?它是如何工作的?

    想象一个场景,你要部署一个服务,然后它对环境有很多依赖,不同的操作系统又是不同的需求,而且还可能遇到有些源不能使用,又得一番折腾,折腾完上线后,假设要在新的环境再来一套,又得再来一遍。那么有没有什么办法可以解决呢?有办法,docker就是干

    15分钟前
    00
  • UCB的硅光MEMS OCS控制技术

    昨天写的旭创与nEye合作了一个64端口硅光OCS一、光电路交换技术概述(一)电交换与分组交换电路交换的概念源于早期的电话交换机,通过物理连接实现设备间的通信,就像早期打电话时,接线员手动连接线路一样。而分组交换则是

    14分钟前
    00
  • 大模型驱动金融数据应用的实战探索

    近年来,人工智能技术的飞速发展正在重塑全球各行各业的生态格局,金融行业作为数据密集型领域,更是首当其冲。大模型凭借其强大的自然语言处理、逻辑推理和生成能力,逐渐成为金融数据应用的核心驱动力。本文将从行业背景与趋势、核心场景重构、产品能力提升

    8分钟前
    00
  • maxwell遇到的一则问题

    结论和原因maxwell的元数据库里面没有存储全部的schema数据(就是少数据了),导致相关表的DDL校验失败。PS:我这里maxwell的作用只是采集库表修改情况的统计粗粒度指标,因为之前maxwell在运行报错的时候,直接修改了pos

    4分钟前
    00
  • Windows Server20192022 Evaluation评估版未激活导致关机问题

    摘要&#xff1a;在安装Windows Server 20192022后&#xff0c;会出现系统版本为 Evaluation 评估版情况&#xff0c;如提示Windows许可证已到期&#xff0c;就

    3分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信