Split an array of objects in to two, based on a value within the object

I have been trying (and struggling) to figure out how I am able to split an array of objects, based on a key value pair

Long story short, I have a list of stations that a train is calling at, and need to separate the stations previous calling points, and the stations future calling points.

The data i’m working with looks like this:

[{station_code: "SOC", station_name: "Southend Central" },
{station_code: "WCF", station_name: "Westcliff On Sea" },
{station_code: "CHW", station_name: "Chalkwell" },
{station_code: "LES", station_name: "Leigh On Sea" },
{station_code: "BEF", station_name: "Benfleet" },
{station_code: "PSE", station_name: "Pitsea" },
{station_code: "BSO", station_name: "Basildon" }]

I’d like to have two new arrays that are split at, for example Leigh On Sea.. like this:

previous stops:

[{station_code: "SOC", station_name: "Southend Central" },
{station_code: "WCF", station_name: "Westcliff On Sea" },
{station_code: "CHW", station_name: "Chalkwell" }]

subsequent stops:

[{station_code: "BEF", station_name: "Benfleet" },
{station_code: "PSE", station_name: "Pitsea" },
{station_code: "BSO", station_name: "Basildon" }]

it would also help to potentially return the current station (leigh on sea) as well as a separate array with the object in it if possible…

I have come across this, however i cant think how i can adjust this to look in to the object to find the station name:

const getAllAfter = (current) => {
    var myArr = new Array("alpha", "beta", "gamma", "delta");
    var i = myArr.indexOf(current);
    return i > -1 ? myArr.slice(0, i) : [];
};

Thanks in advance! 🙂

27 thoughts on “Split an array of objects in to two, based on a value within the object”

  1. Use Array.findIndex() to find the index of separating item, and then use Array.slice() to get the array before, the item, and the array after.

    const splitAt = (predicate, arr) => {
      const index = arr.findIndex(predicate);
    
      return [
        arr.slice(0, index),
        arr[index],
        arr.slice(index + 1)
      ];
    };
    
    
    const data = [{"station_code":"SOC","station_name":"Southend Central"},{"station_code":"WCF","station_name":"Westcliff On Sea"},{"station_code":"CHW","station_name":"Chalkwell"},{"station_code":"LES","station_name":"Leigh On Sea"},{"station_code":"BEF","station_name":"Benfleet"},{"station_code":"PSE","station_name":"Pitsea"},{"station_code":"BSO","station_name":"Basildon"}];
    
    const [before, separator, after] = splitAt(o => o.station_name === 'Leigh On Sea', data);
    
    console.log({ before, separator, after });
    Reply
  2. I have a very sloppy idea so I don’t know if you want to take it, but what you are suggesting is actually very similar to a built-in split method in JS but that works with strings, so you could take your array, JSON.stringify() it, and then take whichever stop you wanted to split it at, make that object a string as well, and then use your string object to split it into an array of separated string objects. Then simply remove the starting and ending brackets from the two array objects and parse it back into an object, voila you have achieved what you wanted

    Example:

    var arr = [{station_code: "SOC", station_name: "Southend Central" },
    {station_code: "WCF", station_name: "Westcliff On Sea" },
    {station_code: "CHW", station_name: "Chalkwell" },
    {station_code: "LES", station_name: "Leigh On Sea" },
    {station_code: "BEF", station_name: "Benfleet" },
    {station_code: "PSE", station_name: "Pitsea" },
    {station_code: "BSO", station_name: "Basildon" }];
    
    var objectToSplitAt = {station_code: "LES", station_name: "Leigh On Sea" };
    var stringObjectToSplitAt = JSON.stringify(objectToSplitAt);
    var stringArr = JSON.stringify(arr);
    var ourSplitStringResult = stringArr.split(stringObjectToSplitAt + ",");
    //Add the brackets to make it proper JSON
    ourSplitStringResult[0] = ourSplitStringResult[0] + "]";
    ourSplitStringResult[1] = "[" + ourSplitStringResult[1];
    var ourFinalSplitArrayOfArrays = ourSplitStringResult.map((el)=>{
        return JSON.parse(el);
    });
    
    Reply

Leave a Comment