Remove items from one array if not in the second a

2019-02-24 22:34发布

问题:

I state that I have tried for a long time before writing this post.

For an InDesign script, I'm working with two array of ListItems. Now I'm trying to remove the items of one array that aren't in the second array, but i'm stuck.

Given that I use the following javascript code (it works great) to remove the equal items between the two arrays :

function check_dupli(arr_A, arr_B) {
    for(var i = arr_B.length - 1; i >= 0; i--) {
        for(var j = 0; j < arr_A.length; j++) {
            if(arr_B[i] === arr_A[j]) {
                arr_B.splice(i, 1);
            }
        }
    }
    arr_B.sort();
}

arr_A = ["a","b","d","f","g"]
arr_B = ["a","c","f","h"]

check_dupli(arr_A, arr_B) --> arr_B = ["c","h"]
check_dupli(arr_B, arr_A) --> arr_B = ["b","d","g"]

I thought to modify it in order to ignore the items not that are not in both arrays, and to obtain what I want, but something is going wrong because I also get the unwanted data :

function get_dupli(arr_A, arr_B, arr_C) {
    for(var e = arr_B.length - 1; e >= 0; e--) {
        for(var k = 0; k < arr_A.length; k++) {
            if(arr_B[e] === arr_A[k]) {
                arr_C.push(arr_B[e]);
            }
        }
    }
    arr_C.sort();
}

arr_A = ["a","b","d","f","g"]
arr_B = ["a","g","k"]
arr_C = ["h"]

get_dupli(arr_A, arr_B, arr_C) --> arr_C = ["a","g","h","k"] instead of --> ["a","g","h"]
get_dupli(arr_B, arr_A, arr_C) --> arr_C = ["a","b","d","f","g","h"] instead of --> ["a","g","h"]

Where I'm wrong? There is another way in pure javascript to solve the problem?

Thanks in advance for any help.

回答1:

You can make use of Array.prototype.filter and Array.prototype.concat to simply it:

arr_A = ["a","b","d","f","g"]
arr_B = ["a","g","k"]
arr_C = ["h"]

function getCommonItems(arrayA, arrayB, result) {
 result = result || [];
 result = result.concat(arrayA.filter(function(item) {
            return arrayB.indexOf(item) >= 0;    
          }));
 return result.sort();
}

alert(getCommonItems(arr_A, arr_B, arr_C).join(", "));
alert(getCommonItems(arr_B, arr_A, arr_C).join(", "));

For the first scenario:

arr_A = ["a","b","d","f","g"]
arr_B = ["a","c","f","h"]

function getDifference(arrayA, arrayB, result) {
 return arrayB.filter(function(item) {
            return arrayA.indexOf(item) === -1;    
  }).sort();
}

alert(getDifference(arr_A, arr_B).join(", "));
alert(getDifference(arr_B, arr_A).join(", "));



回答2:

Do it like this:

//the array which will loose some items
var ar1 = ["a", "b", "c"];
//the array which is the template
var ar2 = ["d", "a", "b"];

var tmpar = [];

for(var i = 0; i < ar1.length; i++){
  if(ar2.indexOf(ar1[i]) !== -1){
    tmpar.push(ar1[i]);
  }
}

ar1 = tmpar;

alert(ar1);

We create a temporary array to store the valid values.

We make sure that the index of the value from the first array is not "-1". If it's "-1" the index is not found and therefore the value is not valid! We store everything which is not "-1" (so we store every valid value).



回答3:

Array.prototype.contains =  function ( object )
{
	var i = 0, n = this.length;
	
	for ( i = 0 ; i < n ; i++ )
	{
		if ( this[i] === object )
		{
			return true;
		}
	}
	
	return false;
}

Array.prototype.removeItem = function(value, global) {
	var idx;
	var  n = this.length;
	while ( n-- ) {
		if ( value instanceof RegExp && value.test ( this[n]) 
		|| this[n] === value ) {
			this.splice (n, 1 );
			if ( !global ) return this;
		}
	}
	return this;
};

arr_A = ["a","b","d","f","g"];
arr_B = ["a","c","f","h"];

var item
while ( item = arr_A.pop() ) {
	arr_B.contains ( item ) && arr_B.removeItem ( item  );
}

arr_B;



回答4:

arr_A = ["a","b","d","f","g"];
arr_B = ["a","c","f","h"];
var newArr = [];
var item
while ( item = arr_B.shift() ) {
	arr_A.contains ( item ) && newArr[ newArr.length ] = item ;
}
newArr;// ["a", "f"];



回答5:

Opsss .... I believed I had given the answer and closed this post ... sorry !!!

Despite all the checks I made, the failure of the mine as your script was caused by a stupid mistake ... the array arr_A passed to the function was a modified copy of the original array.

Thank you all for your concern and help. Sorry again ...