[JavaScript] js取两个数组的交集 取两个数组相同的元素 →→→→→进入此内容的聊天室

来自 , 2019-08-10, 写在 JavaScript, 查看 96 次.
URL http://www.code666.cn/view/e3ea3396
  1. function arrayIntersection ( a, b )
  2. {
  3.         var ai=0, bi=0;
  4.         var result = new Array();
  5.         while ( ai < a.length && bi < b.length )
  6.         {
  7.                 if      ( a[ai] < b[bi] ) { ai++; }
  8.                 else if ( a[ai] > b[bi] ) { bi++; }
  9.                 else /* they're equal */
  10.                 {
  11.                         result.push ( a[ai] );
  12.                         ai++;
  13.                         bi++;
  14.                 }
  15.         }
  16.         return result;
  17. }
  18. console.log ( arrayIntersection ( [1,2,3],[2,3,4,5,6] ) );//[2,3]

回复 "js取两个数组的交集 取两个数组相同的元素"

这儿你可以回复上面这条便签

captcha