在两个JavaScript数组之间查找

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

Find Between 2 JavaScript Arrays

问题

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

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

答案1

得分: 2

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

  1. let array1 = ['2023-04-05','2023-04-06','2023-04-07'];
  2. let array2 = ['2023-04-07'];
  3. let res = array1.filter(x => !array2.includes(x));
  4. 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 -->

  1. let array1 = [&#39;2023-04-05&#39;,&#39;2023-04-06&#39;,&#39;2023-04-07&#39;];
  2. let array2 = [&#39;2023-04-07&#39;];
  3. let res = array1.filter(x =&gt; !array2.includes(x));
  4. 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:

确定