如果不是第二个数组中从一个阵列中删除项目 [英] Remove items from one array if not in the second array

查看:152
本文介绍了如果不是第二个数组中从一个阵列中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

有关在InDesign脚本,我有两个阵列ListItems的工作。现在,我试图删除不属于第二阵列在一个阵列中的项目,但我卡住了。

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.

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

在哪里我错了?有一个在纯JavaScript另一种方式来解决这个问题?

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

感谢您事先的任何帮助。

Thanks in advance for any help.

推荐答案

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

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

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

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 ...

这篇关于如果不是第二个数组中从一个阵列中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆