0

I am using Firebase, so if there is a more efficient way to structure this data in order to be able to query only the cards that have not already been viewed by the logged in user, I am open to going that route as well. Right now I am trying to do the filtering after the fact.

I have an array of all cards included in my application that looks like this:

enter image description here

I have a second array that holds information on all the cards the user has already seen. I want to be able to look through both arrays, and if the cid in Array two matches the $id in Array 1, then remove that object entirely from Array 1.

enter image description here

1
  • you can follow my answer. and it was same as the one you have accepted. Commented Aug 27, 2016 at 5:16

3 Answers 3

9

This is actually very easy to do in a functional way:

array1 = array1.filter(item => array2.every(item2 => item2.cid != item.$id));

Array.prototype.filter() returns, as an array, the elements of an array that cause the supplied function to return true. And our filter-evaluator says 'return true if there is no item in array2 whose CID matches this item's ID'.

Because filter() is returning a new array, there's no need to use splice(); we can just reassign array1 to the newly filtered array.

Sign up to request clarification or add additional context in comments.

Comments

3

Loop through the arrays and use splice to remove the element

for(var i =0; i< array2.length; i++ ) {
   for(var j= 0; j< array1.length;j++) {
     if (array2[i].$id === array1[j].$id) {
      array1.splice(j,1);
      break;
    }
  }

Comments

0

Why dont you use one array of objects for that. keep a object key for that

seen

by default keep seen false. and use firebase-query where the column seen is false.

or you can do something like that after fetching the data in your code.

var resultarray = [];

for(var i =0; i< array1.length; i++ ) {
   var flag = true;
   for(var j= 0; j< array2.length;j++) {
     if (array1[1].$id === array2[2].$id) {
       flag = false;
       break;
    }
  }
  if(flag === true) {
    resultarray.push(array1[i]);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.