我在Node.js中遇到问题.我的问题是两个数组比较.例如;
我原来的阵列是;
var a = ["1","2","3","4","5"];
另一个数组是;
var b = ["3","1","4","6","8","7"];
所以,结果消息我想要的是:"2和5缺少原始数组......"
那么在比较两个数组后如何才能获得此消息?
使用Array#filter
方法过滤数组元素.
var a = ["1", "2", "3", "4", "5"];
var b = ["3", "1", "4", "6", "8", "7"];
console.log(
a.filter(function(v) {
return !b.includes(v);
})
)
// or for older browser
console.log(
a.filter(function(v) {
return b.indexOf(v) == -1;
})
)