Remove items from one array if not in the second array

前端 未结 5 2058
清酒与你
清酒与你 2021-01-21 02:49

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

5条回答
  •  遇见更好的自我
    2021-01-21 03:40

    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;

提交回复
热议问题