输入中有两个数组,
var array1 = [1,2,3,4,5,6];
var array2 = [3,4,6,8];
输出应为:
var finalArray = [1,2,5,8];
请使用javascript向我推荐一个解决方案。 我想迭代array1。
答案 0 :(得分:2)
您可以使用array.prototype.filter
和array.prototype.concat:
var arr1 = [1,2,3,4,5,6];
var arr2 = [3,4,6,8];
var res = [].concat(
arr1.filter(e => !arr2.includes(e)),
arr2.filter(e => !arr1.includes(e))
);
console.log(res);
甚至更短的ES6 spread
:
var arr1 = [1,2,3,4,5,6];
var arr2 = [3,4,6,8];
var res = [...arr1.filter(e => !arr2.includes(e)), ...arr2.filter(e => !arr1.includes(e))];
console.log(res);
答案 1 :(得分:1)
使用filter
和concat
var output = array1.filter( s => !array2.includes(s) )
.concat(
array2.filter( s => !array1.includes(s) ) )
<强>演示强>
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [3, 4, 6, 8];
var output = array1.filter(s => !array2.includes(s)).concat(array2.filter(s => !array1.includes(s)));
console.log(output);
&#13;
<强>解释强>
1)首先,filter
array1
中array2
的值filter
array2
2)其次,array1
concat
中的值add-test-source
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>some directory</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3) <sources>
<source>some directory</source>
...
</sources>
1)和2)
答案 2 :(得分:1)
您可以使用.filter()
过滤数组以包含任何元素,这些元素也包含在另一个数组中,然后使用.concat()
组合它们:
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [3, 4, 6, 8];
var arr1 = array1.filter(e => !array2.find(f => f == e));
var arr2 = array2.filter(e => !array1.find(f => f == e));
var arr = arr1.concat(arr2);
console.log(arr);
&#13;
答案 3 :(得分:1)
您可以尝试使用数组SELECT COUNT( DISTINCT col1 ) ...
和filter()
:
concat()
&#13;