4

i want to filter unique array of objects. my initial array would be

var initialObjArray=[ {lat:10,lon:20},{lat :10,lon:30} ,{lat :10,lon:20}];

my result array would be

var initialObjArray=[ {lat:10,lon:20},{lat :10,lon:30}];
2

2 Answers 2

2

Try like this:

var result =  []
initialObjArray.forEach(item => {
  let count = result.filter(x => x.lat == item.lat && x.lon == item.lon).length

  if(count == 0) {
    result.push(item)
  }
})

Working Demo

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

2 Comments

can you tell me the complexity of this process?
Complexity as in.. can you explain
1

You can use filter() and findIndex() to filter duplicate objects.

var initialObjArray = [{ lat:10, lon:20 }, { lat :10, lon:30 }, { lat :10, lon:20 }];

var unique = initialObjArray.filter((value, index, arr) => {
  return index === arr.findIndex(obj => obj.lat === value.lat && obj.lon === value.lon);
});

console.log(unique);

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.