简体   繁体   English

比较两个数组值(如果包含)并求和

[英]compare two array values (if contains) and get difference

How to compare two array values and get the difference. 如何比较两个数组值并获得差值。

const list1 = ['1xx', '2bbb', '3ggg', '4eee'];
const list2 = ['1xx33', '2333', '3gfffgg', '4eeeooo'];

I'd like to check if list2 values contains list1s 我想检查list2值是否包含list1s

so 1xx33 contains 1xx and 4eeeooo contains 4eee then the result I respect to see is ['2bbb', '3ggg'] ; 所以1xx33 contains 1xx4eeeooo contains 4eee那么我4eeeooo contains 4eee的结果是['2bbb', '3ggg'] ;

const output = list1.filter( function(n) { return !this.has(n) }, new Set(list2) );

Above snippet only gives matches but not contains 以上片段仅提供匹配项,但不包含

For every entry in the first list, you must verify if there is no matching substring in any of the values in the second list. 对于第一个列表中的每个条目,必须验证第二个列表中的任何值中是否没有匹配的子字符串。

const output = list1.filter(s => list2.every(b => (b.indexOf(s)===-1)));

This snippet creates a new list by filtering the first list, removing every entry that has at least one matching substring when compared to the other list. 此代码段通过过滤第一个列表来创建新列表,与其他列表相比,删除每个具有至少一个匹配子字符串的条目。

If you're not familiar with arrow functions ( => ), here's the alternative: 如果您不熟悉箭头功能=> ),则可以选择以下方法:

const output = list1.filter(function(s) { return list2.every(function(b) { return (b.indexOf(s)===-1) })});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM