0

I have an array of objects that look like this:

{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "JPY","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "CHF","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "CHF","to_currency": "EUR","storage": "Cash"}
,{"from_currency": "JPY","to_currency": "CHF","storage": "Bank"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "CHF","to_currency": "USD","storage": "Cash"}
,{"from_currency": "JPY","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "CHF","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Cash"}
,{"from_currency": "EUR","to_currency": "CHF","storage": "Bank"}
,{"from_currency": "EUR","to_currency": "JPY","storage": "Bank"}
,{"from_currency": "CHF","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "CHF","to_currency": "EUR","storage": "Cash"}
,{"from_currency": "JPY","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "USD","to_currency": "JPY","storage": "Bank"}
,{"from_currency": "EUR","to_currency": "USD","storage": "Bank"}

I want it reduced to the list of unique objects:

{"from_currency": "USD","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "JPY","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "CHF","to_currency": "EUR","storage": "Bank"}
,{"from_currency": "CHF","to_currency": "EUR","storage": "Cash"}
,{"from_currency": "JPY","to_currency": "CHF","storage": "Bank"}
,{"from_currency": "CHF","to_currency": "USD","storage": "Cash"}
,{"from_currency": "USD","to_currency": "EUR","storage": "Cash"}
,{"from_currency": "EUR","to_currency": "CHF","storage": "Bank"}
,{"from_currency": "EUR","to_currency": "JPY","storage": "Bank"}
,{"from_currency": "USD","to_currency": "JPY","storage": "Bank"}
,{"from_currency": "EUR","to_currency": "USD","storage": "Bank"}

I have no good idea on how to do it. I've looked through several similar questions, but all of them required a simpler answer. I thought about using a simple string-concat solution to use a Map, but I thought before I resort to something as clunky as this, I'd ask.

Spurious
  • 1,903
  • 5
  • 27
  • 53
  • This is not a duplicate, isn't it obvious from the results that I provided that the result we would receive here would discard way too many objects that should be included. – Spurious Apr 02 '20 at 15:18
  • You can try `arr.filter((obj, i, self) => self.slice(i).filter((_obj) => JSON.stringify(obj) === JSON.stringify(_obj)).length < 2)`. – Moritz Roessler Apr 02 '20 at 15:31
  • Yes, but the solution is the clunky one. I think it's the only viable one after searching more through the web. – Spurious Apr 02 '20 at 15:52
  • Yes, it's a dirty solution but the only one i could think of that fits into the comments. I can post an answer if we get more reopen votes. – Moritz Roessler Apr 02 '20 at 15:57

0 Answers0