我有两个数组变量A和B.
A = [1,2,3,4]
B = [1,3,4,5,7,8]
如何在数组B中使用过滤器,它在A中具有相同的元素,如B = [1,3,4]
?
答案 0 :(得分:3)
将Array#includes
与const A = [1,2,3,4];
let B = [1,3,4,5,7,8];
B = B.filter(item => A.includes(item));
console.log(B);
:
/**
* @Route("/ticker/{coin}/{plat}", name="get_coinTicker_from_platform", options={"expose"=true})
*/
答案 1 :(得分:0)
<强>样本强>
let A = [1,2,3,4],
B = [1,3,4,5,7,8];
let result = B.filter(v => A.indexOf(v) >= 0);
console.log(result);
.as-console-wrapper {max-height: 100% !important;top: 0;}
答案 2 :(得分:0)
你可以使用Array.prototype.filter和Array.prototype.indexOf
array1.filter(value => -1 !== array2.indexOf(value));