Q:

Array Find or Filter based on multiple condition

I have list of data with different data as follows 
[0: {lat: "37.829998", lng: "-122.003152", title: "Allocate", pairStatus: 1, …}
1: {lat: "37.829998", lng: "-122.003152", title: "Commission", pairStatus: 1, …}
2: {lat: "37.829998", lng: "-122.003152", title: "Aggregate", pairStatus: 1, …}
3: {lat: "37.7745586", lng: "-121.9610328", title: "Ship", pairStatus: 1, …}
4: {lat: "37.7745586", lng: "-121.9610328", title: "Ship", pairStatus: 1, …}
5: {lat: "37.7745586", lng: "-121.9610328", title: "Ship", pairStatus: 1, …}
6: {lat: "37.7745586", lng: "-121.9610328", title: "Receive", pairStatus: 1, …}
7: {lat: "37.7745586", lng: "-121.9610328", title: "Receive", pairStatus: 1, …}]

I need output as follows - Group lat and lan with title
[0: {lat: "37.829998", lng: "-122.003152", title: "Allocate,Commission,Aggregate"}
1: {lat: "37.7745586", lng: "-121.9610328", title: "Ship,Ship,Ship,Receive,Receive"}]

A:

You can use Array reduce() to achieve this. Find elements in the accumulator, if an item already exists in the accumulator having the same latitude and longitude then concatenate the title into the existing item.
If item doesn't exist, push a new item into the accumulator having "lat", "lng" and "title" properties

let rawData = [  {lat: "37.829998", lng: "-122.003152", title: "Allocate", pairStatus: 1},
{lat: "37.829998", lng: "-122.003152", title: "Commission", pairStatus: 1},
{lat: "37.829998", lng: "-122.003152", title: "Aggregate", pairStatus: 1},
{lat: "37.7745586", lng: "-121.9610328", title: "Ship", pairStatus: 1},
{lat: "37.7745586", lng: "-121.9610328", title: "Ship", pairStatus: 1},
{lat: "37.7745586", lng: "-121.9610328", title: "Ship", pairStatus: 1},
{lat: "37.7745586", lng: "-121.9610328", title: "Receive", pairStatus: 1},
{lat: "37.7745586", lng: "-121.9610328", title: "Receive", pairStatus: 1}]

let finalData = rawData.reduce((acc, {lat, lng, title}) => {
  let found = acc.find((e) => {
    return lat === e.lat && lng === e.lng
  })
  
  if (found) {
     found.title = `${found.title},${title}`
  }
  else {
    acc.push({lat, lng, title})
  }
  return acc
}, [])

console.log(finalData)

