在两个JavaScript数组之间查找

huangapple go评论68阅读模式
英文:

Find Between 2 JavaScript Arrays

问题

<script>
var array1 = ['2023-04-05','2023-04-06','2023-04-07'];    //array1
var array2 = ['2023-04-07'];    //array2
found1 = array1.find((val, index) => {    //find the date in array1
    return array2.includes(val);
});
$('.show').html(found1);
</script>

result 2023-04-07. How to result like this 2023-04-05, 2023-04-06
英文:
<script>
var array1 = ['2023-04-05','2023-04-06','2023-04-07'];    //array1
var array2 = ['2023-04-07'];    //array2
found1 = array1.find((val, index) => {    //find the date in array1
    return array2.includes(val);
});
$('.show').html(found1);
</script>

result 2023-04-07. How to result like this 2023-04-05, 2023-04-06

答案1

得分: 2

你可以使用Array#filter来获取第一个数组中不在第二个数组中的所有元素。

let array1 = ['2023-04-05','2023-04-06','2023-04-07'];
let array2 = ['2023-04-07'];
let res = array1.filter(x => !array2.includes(x));
console.log(res);
英文:

You can use Array#filter to get all elements in the first array not present in the second.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let array1 = [&#39;2023-04-05&#39;,&#39;2023-04-06&#39;,&#39;2023-04-07&#39;];
let array2 = [&#39;2023-04-07&#39;];
let res = array1.filter(x =&gt; !array2.includes(x));
console.log(res);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月10日 23:15:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定