0

I have the following array

 [{ id: 1,
    type: 'video',
    image: null,
    url: 'https://www.youtube.com/1'
  },
  { id: 2,
    type: 'video',
    image: null,
    url: 'https://www.youtube.com/2' 
  },
  { id: 3,
    type: 'image',
    image: 'https://example-1.url.webp'
  },
  { id: 4,
    type: 'image',
    image: 'https://example-2.url.jpg',
  },
  { id: 5,
    type: 'video',
    image: 'https://www.youtube.com/2',
  }   
]

I am already filtering all the items who are not webp format and the image is null

  const galleryFilter = gallery.filter(
    (item) => item?.image?.indexOf("webp") === -1 || item?.image === null
  );

As you can see there are 2 items (id 2 and id 5 ) with the same url, how can i also filter the item duplicated with the same url in the galleryFilter method ?

8
  • 1
    Does this answer your question? How to remove all duplicates from an array of objects? Commented Nov 23, 2021 at 12:35
  • Not unfortunately Commented Nov 23, 2021 at 12:38
  • 1
    append another filter logic Commented Nov 23, 2021 at 12:42
  • @Koala7 What do you mean? Your question is an exact duplicate. You want to filter by "image" instead of "name", but that's it. The linked question has 75 answers. How can this not help you? Commented Nov 23, 2021 at 12:42
  • Ok so why you are making me this question if you contest my answer Commented Nov 23, 2021 at 12:43

2 Answers 2

1

you can append another filter function to filter by image url, (I have added another object into your data set id: 6)

var gallery =  [{ id: 1,
    type: 'video',
    image: null,
    url: 'https://www.youtube.com/1'
  },
  { id: 2,
    type: 'video',
    image: null,
    url: 'https://www.youtube.com/2' 
  },
  { id: 3,
    type: 'image',
    image: 'https://example-1.url.webp'
  },
  { id: 4,
    type: 'image',
    image: 'https://example-2.url.jpg',
  },
  { id: 5,
    type: 'video',
    image: 'https://www.youtube.com/2',
  },
  { id: 6,
    type: 'video',
    image: 'https://www.youtube.com/2',
  }
];

var galleryFilter = gallery.filter( (item) => item?.image?.indexOf("webp") === -1 || item?.image !== null ).filter((item, i, arr) => i == arr.findIndex(e => e.image == item.image));

console.log(galleryFilter);

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

Comments

0

Use filter and keep track of images with Set

const gallery = [
  { id: 1, type: "video", image: null, url: "https://www.youtube.com/1" },
  { id: 2, type: "video", image: null, url: "https://www.youtube.com/2" },
  { id: 3, type: "image", image: "https://example-1.url.webp" },
  { id: 4, type: "image", image: "https://example-2.url.jpg" },
  { id: 5, type: "video", image: "https://www.youtube.com/2" },
];

const set = new Set();

const galleryFilter = gallery.filter((item) => {
  if (
    (item?.image?.includes("webp") || item?.image === null) &&
    !set.has(item?.image)
  ) {
    set.add(item.image);
    return true;
  }
});

console.log(galleryFilter)

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.