Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

item => !arr1.includes(item) || !arr2.includes(item) 这句话是什么语法?

最近做的一道题,题目是求两个数组的差异,并返回新数组。

我看到其中一个非常简练的答案是这样的:

function diffArray(arr1, arr2) {
  return arr1
    .concat(arr2)
    .filter(
        item => !arr1.includes(item) || !arr2.includes(item)
    )
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

其中的item => !arr1.includes(item) || !arr2.includes(item)我完全看不懂,没见过这是什么语法,求大神指点,谢谢!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

涉及3个点:

  1. 箭头函数

  2. 短路求值

  3. includes

item => !arr1.includes(item) || !arr2.includes(item)

可以做如下翻译:

function (item) {
    if (!arr1.includes(item)) {
        return true;
    }
    
    if (!arr2.includes(item)) {
        return true;
    }
    
    return false;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...