Typescript - group and filters objects in an array according to multiple value

Typescript - group and filters objects in an array according to multiple value



Say we have array like:


[

id: 0,
name: 'A'
longitude: 10
latitude: 12
,

id: 1,
name:'B'
longitude: 10
latitude: 12
,

id: 2,
name: 'C'
longitude: 11
latitude: 10
,

id: 3,
name: 'D'
longitude: 11
latitude: 15

]



I want to get an array of object that have the same longitude and latitude like:


[
[

id: 0,
name: 'A'
longitude: 10
latitude: 12
,

id: 1,
name:'B'
longitude: 10
latitude: 12

]
]



The only way that came to mind is looping through the array to get coordinates and the number of objects there, then getting an subarray for each of these coordinates if the length is greater than 1 but the process is quite complex. I am using this in a redux react app, and this is calculated after each state change before rerendering.



Is there a more effective way of doing this? I want to group those with the same longitude and latitude.




1 Answer
1



You can sort the data by longitude and latitude. Next, you need to loop through the data and push your matching objects to a temporary array as shown below. At the end, you can return your temp array.


longitude


latitude




const arr = [
id: 0,
name: 'A',
longitude: 10,
latitude: 12,
,


id: 2,
name: 'C',
longitude: 11,
latitude: 10,
,

id: 3,
name: 'D',
longitude: 11,
latitude: 15,
,

id: 1,
name: 'B',
longitude: 10,
latitude: 12

];

function cleanData(data)
data.sort(function (a, b)
return a.longitude - b.longitude );
let tempArr = ;
for (let i = data.length - 1; i >=1 ; i--)
if (data[i].hasOwnProperty('longitude'))
if (data[i].longitude === data[i - 1].longitude &&
data[i].latitude === data[i - 1].latitude)
tempArr.push(data[i], data[i - 1]);


else
return;



console.log([...[tempArr]]);


cleanData(arr);





@Ismael123 Is it your expected result?
– Damien Asseya
Sep 3 at 16:05






Cool. I am glad I was able to help. Would you mark this as the correct answer?
– Damien Asseya
Sep 3 at 16:18





Not exactly, the longitude doesn't have to equal the latitude. I'll update the question
– Ismael123
Sep 3 at 16:18





@Ismael123 I rewrote the code based on your change. Are you satisfied?
– Damien Asseya
Sep 3 at 18:00





Awesome man thanks. Sorting it didn't even come my mind
– Ismael123
Sep 3 at 18:50



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)