如果不在第二个阵列中,则从一个阵列中删除项目

时间:2015-04-13 13:42:34

标签: javascript arrays adobe-indesign

我声明我在写这篇文章之前已经尝试了很长时间。

对于InDesign脚本,我正在处理两个ListItem数组。现在我试图删除第二个数组中不存在的一个数组的项目,但是我被卡住了。

鉴于我使用以下javascript代码(效果很好)删除两个数组之间的相等项:

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"]

我想修改它以便忽略那些不在两个数组中的项目,并获得我想要的东西,但是出了点问题,因为我也得到了不需要的数据:

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"]

我哪里错了?纯javascript还有另一种解决问题的方法吗?

提前感谢您的帮助。

5 个答案:

答案 0 :(得分:2)

您可以使用Array.prototype.filterArray.prototype.concat来完成它:

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(", "));

对于第一种情况:

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(", "));

答案 1 :(得分:1)

这样做:

&#13;
&#13;
//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);
&#13;
&#13;
&#13;

我们创建一个临时数组来存储有效值。

我们确保第一个数组的值的索引不是&#34; -1&#34;。如果是&#34; -1&#34;找不到索引,因此该值无效!我们存储的所有东西都不是&#34; -1&#34; (所以我们存储每个有效值)。

答案 2 :(得分:0)

&#13;
&#13;
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;
&#13;
&#13;
&#13;

答案 3 :(得分:0)

&#13;
&#13;
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"];
&#13;
&#13;
&#13;

答案 4 :(得分:0)

Opsss ....我相信我已经给出了答案并关闭了这个帖子......对不起!!!

尽管我做了所有的检查,但我的失败是因为你的脚本是由于一个愚蠢的错误引起的......传递给函数的数组arr_A是原始数组的修改副本。

感谢大家的关心和帮助。再次抱歉......