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)

3
  • // 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. Commented Aug 4, 2020 at 13:28
  • @Yoshi Ok thanks, you have any idea how to filter objects in array? Commented Aug 4, 2020 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? Commented Aug 4, 2020 at 13:34

3 Answers 3

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

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

5 Comments

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.
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)).
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
That means that one of the objects you are trying to stringify has a reference to themselves. See this SO post.
thanks for the help. i posted the code for people with the same question that worked for me.
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

Comments

0
// Convert fetchedImages array to a Set of image names
const fetchedImageNames = new Set(fetchedImages.map(image => image.name));

// Filter images based on whether their names are in the fetchedImageNames Set
const newImages = images.filter(image => !fetchedImageNames.has(image.name));

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.