I have an array like this:
var flightPlanCoordinates = [
{ lat: 37.772, lng: -122.214, status: "walking" },
{ lat: 36.772, lng: -123.214, status: "walking" },
{ lat: 21.291, lng: -157.821, status: "automotive" },
{ lat: -18.142, lng: 178.431, status: "automotive" },
{ lat: -27.467, lng: 153.027, status: "walking" },
{ lat: -26.467, lng: 151.027, status: "walking" },
];
and I want this to be split up into three arrays that have objects that have the same type like this following format:
[{lat: 37.772, lng: -122.214, status: 'walking'},
{lat: 36.772, lng: -123.214, status: 'walking'}]
[{lat: 21.291, lng: -157.821, status: 'automotive'},
{lat: -18.142, lng: 178.431, status: 'automotive'}]
[{lat: -27.467, lng: 153.027, status: 'walking'}
{lat: -26.467, lng: 151.027, status: 'walking'}]
I cannot just use groupBy to handle this, any ideas?
You could use Array#reduce()
with a group change if status is changing.
var flightPlanCoordinates = [{ lat: 37.772, lng: -122.214, status: 'walking' }, { lat: 36.772, lng: -123.214, status: 'walking' }, { lat: 21.291, lng: -157.821, status: 'automotive' }, { lat: -18.142, lng: 178.431, status: 'automotive' }, { lat: -27.467, lng: 153.027, status: 'walking' }, { lat: -26.467, lng: 151.027, status: 'walking' }],
grouped = flightPlanCoordinates.reduce(function (r, a, i) {
if (!i || r[r.length - 1][0].status !== a.status) {
return r.concat([[a]]);
}
r[r.length - 1].push(a);
return r;
}, []);
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');
Use for-loop, When the current element status is different from previous elements status, Then push the slice
of array to output array.
const split = (arr) => {
const output = [];
let last = 0;
for (let i = 1; i <= arr.length; i++) {
if (arr[i]?.status !== arr[i - 1]?.status) {
output.push(arr.slice(last, i));
last = i;
}
}
return output;
};
var flightPlanCoordinates = [
{ lat: 37.772, lng: -122.214, status: "walking" },
{ lat: 36.772, lng: -123.214, status: "walking" },
{ lat: 21.291, lng: -157.821, status: "automotive" },
{ lat: -18.142, lng: 178.431, status: "automotive" },
{ lat: -27.467, lng: 153.027, status: "walking" },
{ lat: -26.467, lng: 151.027, status: "walking" },
];
console.log(split(flightPlanCoordinates));
First we need to create a groupBy
function as described in Nina's Answer. Then attach this function to the prototype of the array object so it becomes available globally like map and reduce. It can then be accessed using array.groupBy(key)
. The newly defined groupBy
function will be available for all other arrays as well.
var flightPlanCoordinates = [
{ lat: 37.772, lng: -122.214, status: "walking" },
{ lat: 36.772, lng: -123.214, status: "walking" },
{ lat: 21.291, lng: -157.821, status: "automotive" },
{ lat: -18.142, lng: 178.431, status: "automotive" },
{ lat: -27.467, lng: 153.027, status: "walking" },
{ lat: -26.467, lng: 151.027, status: "walking" },
];
Array.prototype.groupBy = function(key) {
return this.reduce(function (r, a, i) {
if (!i || r[r.length - 1][0][key] !== a[key]) {
return r.concat([[a]]);
}
r[r.length - 1].push(a);
return r;
}, []);
};
console.log(flightPlanCoordinates.groupBy("status"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With