-3

I tried to keep it simple to get the idea but now I realize perhaps mine is a little more complicated.

this for loop runs:

for (var j = 0; j < _USERS.length; j++){
      console.log(_USERS[j].useruid);
    }

gives me:

BLRlXKIeWkanHiSCXbBCFRTIaqk1
CMMpvodHJAYdR4RvI5RxZtJ8llW2
sF4gWbZvAMPmrbeHsKzln7LowOx2
xcBZNxuAahWY6kXe7S3ZJgpDbPm1

this for loop runs:

for (var i = 0; i < promises.length; i++){
      console.log(promises[i][0].likedUseruid);
    }

gives me:

xcBZNxuAahWY6kXe7S3ZJgpDbPm1

how do I then removes ^^^^ from the first array so it reads:

BLRlXKIeWkanHiSCXbBCFRTIaqk1
CMMpvodHJAYdR4RvI5RxZtJ8llW2
sF4gWbZvAMPmrbeHsKzln7LowOx2

without the one ending Pm1 being there?

These are all objects rather than elements as well

Waterman1
  • 135
  • 1
  • 3
  • 11
  • 5
    Possible duplicate of [Javascript arrays: remove all elements contained in another array](http://stackoverflow.com/questions/19957348/javascript-arrays-remove-all-elements-contained-in-another-array) – David R Sep 15 '16 at 12:41
  • ^^ the solutions there use `indexOf`, which compares with `===`, which would *not* work for the example arrays above, which contain *equivalent* "Rich" objects, but not the *same* "Rich" object. – T.J. Crowder Sep 15 '16 at 12:44
  • Will the "Rich" object literally be the **same object**, or just equivalent ones? Your question contains equivalent ones, not the *same* one, in the arrays. – T.J. Crowder Sep 15 '16 at 12:45
  • CHANGED MY CODE GUYS. may still be a duplicate but I believe now it is a little more complicated than the duplicated question – Waterman1 Sep 15 '16 at 14:09

4 Answers4

1

Try something like the following:

var myArray = [{name:"Rich", gender:"male"},
         {name:"Hannah", gender:"female"}];

var mySecondArray = [{name:"Rich", gender:"male"},
             {name:"Lauren", gender:"female"}];

var mySecondArrayNames = mySecondArray.map(x => x.name);

console.log(myArray.filter(x => mySecondArrayNames.indexOf(x.name) === -1));
Phylogenesis
  • 7,775
  • 19
  • 27
0

use lodash difference by

_.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');

https://lodash.com/docs/4.15.0#difference

Sathish
  • 337
  • 1
  • 9
0

To begin with, since you can't compare your literal objects in js directly you will need a function that compares key-values for the objects (I suggest lodash isEqual),

then you can filter using array.some and compare the objects:

myArray = [{name:"Rich", gender:"male"},
             {name:"Hannah", gender:"female"}];

mySecondArray = [{name:"Rich", gender:"male"},
                 {name:"Lauren", gender:"female"}];

var res = myArray.filter(function(x){
  return ! mySecondArray.some(function(z){
    return _.isEqual(z,x)
  })
})

console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
maioman
  • 18,154
  • 4
  • 36
  • 42
0

This is the most concise solution. Using lodash, you can do the following:

var myArray = [{name:"Rich", gender:"male"},
             {name:"Hannah", gender:"female"}];

var mySecondArray = [{name:"Rich", gender:"male"},
                 {name:"Lauren", gender:"female"}];

var result = _.differenceWith(myArray, mySecondArray, _.isEqual);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

differenceWith function returns all of the elements in the first array that are not contained in the second. The third argument is a function that defines when one element is equal to another. You could provide your own function for this, but lodash already has the isEqual function built in, which compares all field names and values of two objects.

As mentioned previously. lodash has a lot of great resources for doing these kinds of tasks on arrays and objects. You should definitely take a look at the documentation. lodash documentation

Hayden Braxton
  • 1,151
  • 9
  • 14