3

I'm trying to subtract, i.e. remove, multiple objects, namely the objects of array2 found in array1 so that the end result is that array2 only contains the "jean" object.

Help! I'm lost.

I tried multiple times code like the following example but it didn't worked.

array1= [{"caracname" : "charles"}, {"caracname" : "kevin"}]
array2= [{"caracname" : "charles"}, {"caracname" : "kevin"}, {"caracname" : "jean"}]
            
            array1.forEach(function (k) {
                delete array2[k];
            });
            
            console.log(array2);
slevy1
  • 3,797
  • 2
  • 27
  • 33
jean
  • 73
  • 2
  • 8
  • Possible duplicate of [Compare two Javascript Arrays and remove Duplicates](https://stackoverflow.com/questions/14930516/compare-two-javascript-arrays-and-remove-duplicates) – John Ruddell Oct 15 '17 at 18:27

4 Answers4

3

A bit easier with a libraries like Lodash :

var array1 = [{"caracname" : "charles"}, {"caracname" : "kevin"}];
var array2 = [{"caracname" : "charles"}, {"caracname" : "kevin"}, {"caracname" : "jean"}];

var array3 = _.differenceWith(array2, array1, _.isEqual);

console.log( JSON.stringify(array3) );

// or 
_.pullAllWith(array2, array1, _.isEqual);

console.log( JSON.stringify(array2) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

A bit less efficient alternative without library (might not work for objects with more than one property) :

var array1 = [{"caracname" : "charles"}, {"caracname" : "kevin"}];
var array2 = [{"caracname" : "charles"}, {"caracname" : "kevin"}, {"caracname" : "jean"}];

var j = JSON.stringify, array3 = array2.filter(e => array1.map(j).indexOf(j(e)) < 0);

console.log( j(array3) );
Slai
  • 22,144
  • 5
  • 45
  • 53
2

Here is how to do this:

function objectsEqual(o1, o2) {
    return o1.caracname === o2.caracname
}

function subtractArrays(a1, a2) {
    var arr = [];
    a1.forEach((o1) => {
       var found = false;
       a2.forEach((o2) => {
           if (objectsEqual(o1, o2)) {
                found = true;
           }  
       });
       if (!found) {
           arr.push(o1);
       }
    })
    return arr;
}

console.log(subtractArrays(array2, array1));

fixed in plunker: https://plnkr.co/edit/vA6oWg9R44f6k7ylyFh6?p=preview

slevy1
  • 3,797
  • 2
  • 27
  • 33
Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98
1

As it turns out deleting an array element is not as easy as deleting a variable; see here. The "deleted" element is not deleted but acquires an undefined value while the length of the array stays the same.

Found some helpful advice here. So, you may use array2's splice method which according to MDN handily does the following:

The splice() method changes the contents of an array by removing existing elements and/or adding new elements.

The following snippet works irrespective of the order of the elements in each array, as follows:

var arr1 = [{
  "caracname": "charles"
}, {
  "caracname": "kevin"
}];

var arr2 = [{
  "caracname": "charles"
}, {
  "caracname": "jean"
}, {
  "caracname": "kevin"
}];

var tmp1 = "",
    tmp2 = "";

for (var k = 0, key = "", maxLength = arr1.length; k < maxLength; k++) {
  key = Object.keys(arr1[k])[0];
  
  for (var j = 0, maxLength_2 = arr2.length; j < maxLength_2; j++) {
  
    tmp2 = arr2[j][key];
    tmp1 = arr1[k][key];
  
  if (tmp2 === tmp1) {
      arr2.splice(j, 1);
      maxLength_2--; // array2 length now one less
      break;
    }// end if
  }// end inner-for
}// end outer-for

console.log(arr2);

I've revised this example after running the code here, altering some variable names as a result.

As an aside, you should declare your arrays with var or optionally let; more info here and here.

slevy1
  • 3,797
  • 2
  • 27
  • 33
  • 1
    "proper" object comparison is not easy https://stackoverflow.com/questions/1068834/object-comparison-in-javascript (that's why I didn't even attempt it in my answer, but just compared their JSON string representations, and Nayish compares just the values of the caracname properties). Pasting JavaScript in the TypeScript https://www.typescriptlang.org/play/ or similar editor, might be helpful to notice some of the issues (not sure if using the array from Object.keys as string `[key]` is intentional) – Slai Oct 17 '17 at 16:04
  • @Slai Thanks for this info. I fixed the code so that now var key holds a string value. Tested code and it seems okay now when I run it at the Typescript website. – slevy1 Oct 17 '17 at 22:18
0

Use this code: [ advised by @slevy1 ]

var array1= [{"caracname" : "charles"}, {"caracname" : "kevin"}]
var array2= [{"caracname" : "charles"}, {"caracname" : "kevin"}, {"caracname" : "jean"}]

array1.forEach(function (k) {
    var index = array1.valueOf(k);
    array2.splice(index, 1);
});

array2.forEach(function (o) {
    console.log(o.caracname);
});

codepen link

fatih
  • 330
  • 8
  • 17
  • I ran your code here https://codepen.io/anon/pen/KXGZPv?editors=1111 and didn't get the intended result. – slevy1 Oct 15 '17 at 20:53
  • Hi @slevy1, thank you for your effort. I've edited(fixed) my code and it is working now. Please test again bro. – fatih Oct 15 '17 at 21:22
  • @Fatih `index` is not a number (try `console.log(index)`) and array2.splice just removes the first item – Slai Oct 15 '17 at 21:43
  • 1
    @Fatih Indeed your code does work, so I give you my upvote. I ran your code at https://codepen.io/anon/pen/wrYmrv?editors=1111 and made a few changes, namely your arrays should be declared with either var or let. Secondly I changed the "k" to "o" for the parameter of the anonymous function to remind users that the parameter is an object. – slevy1 Oct 16 '17 at 00:48
  • Thank you @slevy1 for your advice. – fatih Oct 16 '17 at 15:39