search

Home  >  Q&A  >  body text

Find elements in two 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) => {    //在array1中查找日期
    return array2.includes(val);
});
$('.show').html(found1);
</script>

The result is 2023-04-07. How to get results similar to 2023-04-05, 2023-04-06

P粉242126786P粉242126786437 days ago480

reply all(1)I'll reply

  • P粉477369269

    P粉4773692692023-09-12 14:01:47

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

    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);

    reply
    0
  • Cancelreply