0

If any values of a specific property matches another property in an array, I only want one of them returned.

Meaning I don't want any duplicates. I don't need to return JUST the value of the name property, if the name property matches I want to filter it out of the returned array and keep the objects with differing "name" property values

For example, the spotify album search is returning this:

const array = [{
  images: Array(3) [ {…}, {…}, {…} ],
  name: "KIDS SEE GHOSTS",
  release_date: "2018-06-12",
  release_date_precision: "day",
}, {
  images: Array(3) [ {…}, {…}, {…} ],
  name: "KIDS SEE GHOSTS",
  release_date: "2018-06-08",
  release_date_precision: "day",
}];

I would like to return an array of objects that have unique values for the "name" property.

naveen
  • 53,448
  • 46
  • 161
  • 251
  • Thanks but I want the whole object not just the value. I want to filter out any objects that have matching "name" properties and return the WHOLE object, not just the value – user39483893 Oct 06 '22 at 20:32
  • Do you know which one you want when there are more than one? Like the first one? Or some merge of all of them? – danh Oct 06 '22 at 20:33
  • There is [an answer on the proposed duplicate](https://stackoverflow.com/a/58429784/215552) that returns the whole object. – Heretic Monkey Oct 06 '22 at 20:34
  • @danh I want all the objects that don't have matching name properties. The whole object. If the name property has the same value, I don't want it in the returned array. – user39483893 Oct 06 '22 at 20:35
  • Use new `Set()` method to remove duplicates – Khaled Ayed Oct 06 '22 at 20:36
  • To be clear @user39483893, if you find two with the same name (understanding you what the whole object), do you want neither of those two? Or the first one, or the last (as the dup answer provides), or some other criterion to decide? – danh Oct 06 '22 at 20:37
  • Please clear your question. Give a sample input and output that helps to understand the problem. – Arifur Rahaman Oct 06 '22 at 20:39
  • @HereticMonkey the first answer is really good, the linked solution. It's just hard for me to understand it... – user39483893 Oct 06 '22 at 20:42
  • @ArifurRahaman I don't know how else you want me to explain it. If there are objects that have the same value for "name", I want it filtered out and only one of them in the final array. An array with objects that have unique values for the "name" property. – user39483893 Oct 06 '22 at 20:44
  • @user39483893 - I don't love the readability of that answer either, but what it says is to turn the array into an object keyed by the prop we don't want to duplicate (like `name`), since there can only be one prop for each name, the dup gets overwritten until the last dup. The `.values()` of that object will now be an array with only the last instance of each duplicate – danh Oct 06 '22 at 20:45
  • And @user39483893 - "I only want one of them..." still never answered my question: "which one?", but it sounds like you've got what you need via the dup (I hope) – danh Oct 06 '22 at 20:46
  • @danh wow bro, that helped a lot. So he's turning the whole thing into an object first, I'm guessing that's what "...new Map" is doing? So the property would be the value of "name" and the value of "name" would now be the original, whole object? – user39483893 Oct 06 '22 at 20:50
  • Yes. `item[key]` item is like your album, key should be set like let `key = 'name'`. If you logged that map before asking for .values(), you'd see `{ "KIDS SEE GHOSTS" : { images: [...], name: "KIDS SEE GHOSTS", ... }` the value of the name prop is a key. – danh Oct 06 '22 at 20:56
  • @danh I just don't quite understand how it's ridding of the duplicate. I'm guessing it's somewhere after the map method is used, it's just hard for to understand. I'll try using that solution one line at a time, console logging it, tomorrow. Thanks bro – user39483893 Oct 06 '22 at 21:01
  • @user39483893 ask yourself, how many keys called "KIDS SEE GHOSTS" can a map have? – danh Oct 06 '22 at 21:19

0 Answers0