[英]compare two array values (if contains) and get difference
如何比較兩個數組值並獲得差值。
const list1 = ['1xx', '2bbb', '3ggg', '4eee'];
const list2 = ['1xx33', '2333', '3gfffgg', '4eeeooo'];
我想檢查list2值是否包含list1s
所以1xx33 contains 1xx
而4eeeooo contains 4eee
那么我4eeeooo contains 4eee
的結果是['2bbb', '3ggg']
;
const output = list1.filter( function(n) { return !this.has(n) }, new Set(list2) );
以上片段僅提供匹配項,但不包含
對於第一個列表中的每個條目,必須驗證第二個列表中的任何值中是否沒有匹配的子字符串。
const output = list1.filter(s => list2.every(b => (b.indexOf(s)===-1)));
此代碼段通過過濾第一個列表來創建新列表,與其他列表相比,刪除每個具有至少一個匹配子字符串的條目。
如果您不熟悉箭頭功能 ( =>
),則可以選擇以下方法:
const output = list1.filter(function(s) { return list2.every(function(b) { return (b.indexOf(s)===-1) })});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.