英文:
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 = ['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);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论