0

I have a problem with the javascript filter function.
This is my idea of how it should work.

  1. I need to check if an object is in the fetchedImage array (at first all the images are not in the array because the array is empty).
  2. so we push all images that are new (not in the fetchedImage array) into the queue array.
  3. the second time we check if the objects are in the array, they will be in the array so no images go in the queue array.

my code results show that the queue is keeping to grow without adding new images.
my code:

let images = res.items;
if(images)
{
    // should return items that are not in the array
    let newImages = images.filter(image => {
        return fetchedImages.includes(image) == false; // ps: (image is object)
    })
                
    // add all new images into the queue array
    queue = [].concat(queue, newImages);
}

(I probably did something wrong in the filter function but i cannot fix it)

Eric
  • 361
  • 6
  • 25
  • `// ps: (image is object)` => if they are not exactly the same objects, meaning the same reference to memory, `includes` will return false. Example: `[{a: 42}].includes({a: 42})` will always be `false`. – Yoshi Aug 04 '20 at 13:28
  • @Yoshi Ok thanks, you have any idea how to filter objects in array? – Eric Aug 04 '20 at 13:31
  • See n9iels answer. Personally I'd suggest going for a unique property of those images. Maybe they have an id, name, path? – Yoshi Aug 04 '20 at 13:34

2 Answers2

1

In JavaScript you cannot safely compare two objects. Either use a unique property of the image objects to compare, or see this SO post for other solutions: Object comparison in JavaScript

n9iels
  • 867
  • 7
  • 21
  • Thanks @n9iels, I looked at the other post, but that did not say how to compare an object to an object in the array. i cannot Json.stringify() my object in the array. – Eric Aug 04 '20 at 13:34
  • If you cannot use a property of the `image` object (would recommend that) you do something like: `return fetchedImages.map(i => Json.stringify(i)).includes(Json.stringify(image))`. – n9iels Aug 04 '20 at 13:41
  • that gives the following error: Uncaught (in promise) TypeError: Converting circular structure to JSON --> starting at object with constructor 'q' | property 'firebase_' -> object with constructor 'Object' | property 'apps' -> object with constructor 'Array' --- index 0 closes the circle – Eric Aug 04 '20 at 13:49
  • That means that one of the objects you are trying to stringify has a reference to themselves. [See this SO post](https://stackoverflow.com/questions/27101240/typeerror-converting-circular-structure-to-json-in-nodejs). – n9iels Aug 04 '20 at 13:53
  • thanks for the help. i posted the code for people with the same question that worked for me. – Eric Aug 04 '20 at 14:04
0

if anyone has this same problem. i found a working solution with this specific code:

let newImages = images.filter(image => {
    return fetchedImages.find(fetchedImage => fetchedImage.name == image.name) == undefined
})  

just use the find method

Eric
  • 361
  • 6
  • 25