从两个数组中获取唯一值并将它们放在另一个数组中 [英] Get the unique values from two arrays and put them in another array
本文介绍了从两个数组中获取唯一值并将它们放在另一个数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在javascript中有两个数组 - :
I have two arrays in javascript -:
var array1 = ['12','1','10','19','100'];
var array2 = ['12','10','19'];
我需要一种方法从两个数组中获取唯一数据并将它们放入array3
Array3
应为 - :
I need to a method to get the unique from two arrays and put them in array3
Array3
should be -:
var array3 = ['1','100'];
感谢您的帮助。
推荐答案
var array3 = array1.filter(function(obj) { return array2.indexOf(obj) == -1; });
阵列#过滤器上的MDN: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter
MDN on Array#filter: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter
包含旧版浏览器的polyfill。
Includes a polyfill for older browsers.
这篇关于从两个数组中获取唯一值并将它们放在另一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文