1

I have the following array of objects:

[{x: "1", y: "2", test: "9.000", class: "low-latency"},
{x: "2", y: "3", test: "9.000", class: "low-latency"},
{x: "22", y: "22", test: "0.000", class: "low-latency"},
{x: "22", y: "22", test: "127.000", class: "high-latency"},
{x: "5", y: "6", test: "96.000", class: "high-latency"},
{x: "6", y: "7", test: "66.000", class: "low-latency"},
{x: "7", y: "8", test: "71.000", class: "high-latency"}]

This is a part of a react reducer so this gets updated every x number of seconds based on the data being returned from a socket.

What I need to do is make sure that when x and y have the same value - there should be just one of them. What is the best way to achieve this?

3
  • Can't this be done when the list is updated? That would be the most efficient way. Doing it after the fact would be much less desirable. Commented Jan 9, 2018 at 15:28
  • Also, I assume you would actually need to mutate the original array instead of creating a new, filtered array, since it seems to be shared. Is that right? Commented Jan 9, 2018 at 15:32
  • 1
    yup, thats correct... Commented Jan 9, 2018 at 15:33

1 Answer 1

2

Simplest way is to use _.uniqBy and hash, eg.

_.uniqBy(arr, ({x,y}) => x+':'+y)
Sign up to request clarification or add additional context in comments.

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.