0

I have this annoying conundrum in firebase whereby I need to compare an object being returned to me with an array

I need this method filling out

const removeAlreadySeenUsersFromQueue = (newUsers, likedUsers) => {

}

basically newUsers is an array of objects. each object has an id that I'm interested in.

then likedUsers is coming back as an array of objects of objects. looking something like this: [{object1: {}, object2: {}, object3: {}] basically an array of length one. inside each object there is an id key that I'm interested in. I basically want to compare both of those and return an array when someone's id only appers in newUsers and not in likedUsers. I figure I need to use object.keys() potentially but currently I can't get anything to work

example below:

[{id: 4444}, {id: 5555}, {id: 6666}]

[{object1: {id: 4444}, object2: {id: 55555}, object3: {id: 121241}}]

after comparing these 2, I would only want {id: 6666} returned to me.

The Walrus
  • 1,148
  • 6
  • 28
  • 46
  • It would be good if you could share what it is you've already done so that we can point out where it is you're going wrong. – danielcooperxyz Jan 31 '18 at 16:51
  • If [the question you posted earlier](https://stackoverflow.com/questions/48539962/javascript-return-array-from-2-arrays-removing-duplicates) didn't solve your problem then you're going to have to be clearer about what you want the end result to be. Can you post an object or array that shows *exactly* what you want to get out of this? – Reinstate Monica Cellio Jan 31 '18 at 16:52
  • @Archer sorry yes I realised the data structure was different – The Walrus Jan 31 '18 at 16:54
  • It's no problem - just saying that if it's not clear you won't get a better answer ;) – Reinstate Monica Cellio Jan 31 '18 at 16:54
  • @Archer ok made an example. does that help? – The Walrus Jan 31 '18 at 16:57
  • No. That's still not clear. What if there was `{id: 7777}` in the original collection - how would the expected response look then? Is the expected response an array of objects, or an object with multiple properties? Can you just post some javascript showing variables that are exactly what you're putting in and a variable showing exactly what you want to get out, preferably with more than one result. – Reinstate Monica Cellio Jan 31 '18 at 16:59
  • sorry, I would like an array of objects returned. if `{id: 7777}` then I'd want that returned as a seperate objects. the reason im getting into difficulty is because firebase is returning me 2 different types – The Walrus Jan 31 '18 at 17:00

3 Answers3

0

You can use reduce to get the ids and then use filter along with some function.

The reduce could return repeated elements [666, 555, 666, 777], but that's is out of scope because your example doesn't have repeated ids.

var source = [{ id: 4444 }, { id: 5555 }, { id: 6666 }, { id: 123456 }];
var content = [{ object1: { id: 4444 }, object2: { id: 5555 }, object3: { id: 121241 }}];

var ids = content.reduce((acc, curr) => [...acc, ...Object.keys(curr).map((k) => curr[k].id)] , []);

var result = source.filter((e) => {
  return !ids.some((c) => {
    return e.id === c;
  });
});

console.log(result);

Resources

Community
  • 1
  • 1
Ele
  • 33,468
  • 7
  • 37
  • 75
0

This will work with the presented data, and I added { id: 7777 } to the first input, just to show it handling multiple matches (may or may not be relevant).

// input vars
var ar1 = [{id: 4444}, {id: 5555}, {id: 6666}, {id: 7777}];
var ar2 = [{object1: {id: 4444}, object2: {id: 5555}, object3: {id: 121241}}];

// output
var ar3 = ar1.filter(function(o) {
    // assume we're adding the element from ar1
    var result = true;

    // if we find the element from ar1 in ar2 then we won't add it
    Object.keys(ar2[0]).forEach(function(key) { if (ar2[0][key].id == o.id)     result = false; });

    // complete the filter function, adding elements not found
    return result;
});

// log the output
console.log(ar3);
Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
-1

You said that your array has length of 1? you mean: [{}]? Change that to be [{}, {}, {}] and iterate over this with for (element of array) - for of loop. Or if it has to be one object that holds them all, iterate over it via for in loop. I mean, for (element in array[0]).

EDIT Now that I saw your example, here's a snippet:

const firstArray = [{id: 4444}, {id: 5555}, {id: 6666}];
const secondArray = [{object1: {id: 4444}, object2: {id: 55555}, object3: {id: 121241}}];

for (let object in secondArray[0]) {
   if (firstArray.some(el => el.id === secondArray[0][object].id)} {
      //do something
   }
}
Pistolpete .
  • 1,118
  • 1
  • 8
  • 17