Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove items from one array if not in the second array

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.

like image 575
Alessio Tonarelli Avatar asked Mar 17 '26 22:03

Alessio Tonarelli


2 Answers

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(", "));
like image 126
mohamedrias Avatar answered Mar 20 '26 12:03

mohamedrias


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

like image 34
OddDev Avatar answered Mar 20 '26 13:03

OddDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!