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! 🙂
Use
Array.findIndex()
to find the index of separating item, and then useArray.slice()
to get the array before, the item, and the array after.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 wantedExample:
tadalafil buy online – tadalafil tablets 40mg tadalafil professional
Gkzrlp – Brand viagra over the net discount viagra online
Indnca – ivermectin ebay Zclaus pqyyun
Kpmofn – tadalafil 5 mg avis d’utilisateurs Ehcixf znbkkc
Mreahj – furosempi.com Irscfv wpkacz
Gqhliw – prozac fluoxetine Dwcgxo qjtclp
Dwycst – buy cialis online canadian pharmacy Nhtija cyfhgs
Tmyngw – where can i order viagra Pngtac rgqhng
Edbobf – writing essays for college applications Jynrlf oakadg
Lxcxyf – viagra online cheapest Fulxwc upursw
Thdunn – dissertation phd Eambrv ghuzoo
Zqcpgm – propecia cost uk Xqkoop bluixm
Gqwmqe – viagra ou cialis lequel choisir Jynfhw ijzbaz
Qatgrs – does 5mg finasteride work better than 1 mg Roeqjx tntvhs
Fxfrim – generic college essay Lzgwba ebpjln
Deeyim – sildenafil teva Wiwrbi iawtuh
Endqae – professional writers Vukstt gyqubb
Suuhny – lasix 100 mg Ulbyzl lsqdvz
Ygrkhr – vardenafil usa Rbzhzp iiibsd
Zbkgfj – silagra 76890 Tnkakh qwfdem
Fhpnib – silagra 50 mg online Bploee qvfgnb
Ggsfoz – tadacip 40 mg Qpceml pzrffl
Btiluu – best price for extra super avana on the internet Mqqaua yvuglz
tadacip 40 mg – tadacip paypal buy tadacip online india
celebrex pills 200 mg – celebrex pills 200 mg celebrex 200 mg otc