如果它有另一个数组的元素,是否还有其他更好的方法可以从一个数组中删除重复项?
<script>
var array1 = new Array("a","b","c","d","e","f");
var array2 = new Array("c","e");
for (var i = 0; i<array2.length; i++) {
var arrlen = array1.length;
for (var j = 0; j<arrlen; j++) {
if (array2[i] == array1[j]) {
array1 = array1.slice(0, j).concat(array1.slice(j+1, arrlen));
}
}
}
alert(array1);
</script>
答案 0 :(得分:99)
array1 = array1.filter(function(val) {
return array2.indexOf(val) == -1;
});
或者,随着ES6的可用性:
array1 = array1.filter(val => !array2.includes(val));
答案 1 :(得分:3)
使用$SOLR_HOME/logs
Array.splice()
答案 2 :(得分:3)
由于超出我的原因,诀窍是将外循环向下循环(i--)并将内循环向上循环(j ++)。
见下面的例子:
function test() {
var array1 = new Array("a","b","c","d","e","f");
var array2 = new Array("c","e");
for (var i = array1.length - 1; i >= 0; i--) {
for (var j = 0; j < array2.length; j++) {
if (array1[i] === array2[j]) {
array1.splice(i, 1);
}
}
}
console.log(array1)
}
我怎么知道这个?见下文:
for( var i =myArray.length - 1; i>=0; i--){
for( var j=0; j<toRemove.length; j++){
if(myArray[i] === toRemove[j]){
myArray.splice(i, 1);
}
}
}
或
var myArray = [
{name: 'deepak', place: 'bangalore'},
{name: 'chirag', place: 'bangalore'},
{name: 'alok', place: 'berhampur'},
{name: 'chandan', place: 'mumbai'}
];
var toRemove = [
{name: 'deepak', place: 'bangalore'},
{name: 'alok', place: 'berhampur'}
];
for( var i=myArray.length - 1; i>=0; i--){
for( var j=0; j<toRemove.length; j++){
if(myArray[i] && (myArray[i].name === toRemove[j].name)){
myArray.splice(i, 1);
}
}
}
alert(JSON.stringify(myArray));
就此而言,是否有人能够解释为什么外环需要向下循环( - )?
祝你好运!答案 3 :(得分:1)
使用Set.prototype构造函数:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
let array1 = Array('a', 'b', 'c', 'd', 'e', 'f')
let array2 = Array('c', 'e', 'g')
let concat = array1.concat(array2) // join arrays => [ 'a', 'b', 'c', 'd', 'e', 'f', 'c', 'e', 'g' ]
// Set will filter out duplicates automatically
let set = new Set(concat) // => Set { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }
// Use spread operator to extend Set to an Array
let result = [...set]
console.log(result) // => [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ]
答案 4 :(得分:0)
window.onload = function () {
var array1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'];
var array2 = ['c', 'h', 'k'];
var array3 = [];
var SecondarrayIndexcount = 0;
for (var i = 0; i < array1.length; i++) {
for (var j = 0; j < array2.length; j++) {
if (array1[i] !== array2[j]) {
if (SecondarrayIndexcount === (array2.length - 1)) {
array3.push(array1[i]);
SecondarrayIndexcount = 0;
break;
}
SecondarrayIndexcount++;
}
}
}
for (var i in array3) {
alert(array3[i]);
}
}
</script>
答案 5 :(得分:0)
这是我的解决方法
array1 = array1.filter(function(val) {
return array2.indexOf(val.toString()) == -1;
});
答案 6 :(得分:0)
这是我的解决方案,用于删除ES6中的重复项。
let foundDuplicate = false;
existingOptions.some(existingItem => {
result = result.filter(item => {
if (existingItem.value !== item.value) {
return item;
} else {
foundDuplicate = true;
}
});
return foundDuplicate;
});
之所以使用这种方法,是因为在我的情况下,我有对象数组,而indexOf却有问题。
答案 7 :(得分:0)
arr1 = arr1.filter(val =>!arr2.includes(val));