Rahul, a college student, travels daily from Bhandup to Vadala for his classes. His journey involves changing trains at Kurla, similar to how JavaScript array methods help us modify data.
Let's follow his journey and relate it to JavaScript array methods.
🚇Phase 1: Bhandup to Kurla (Central Line)
1️⃣ push( ) → Friends Join the Train at Different Stations
Rahul borads the local train at Bhandup. As the train moves,his friends Neha and Amit board the train at Vikroli Station. This is like using push( ) method to add elements into array.
const trainCompanion=["Rahul"];
trainCompanion.push("Neha","Amit");
//output ["Rahul","Neha","Amit"]
2️⃣ pop( ) → A Friend Gets off the Train
At Ghatkoper, Amit realises that he forgot his college ID at home and gets off. This is like using pop( ) method to remove element from end.
trainCompanion.pop()
//output: ["Rahul","Neha"]
Rahul and Neha continue their journey…
3️⃣ slice( ) → Selecting Part of Journey
Rahul wants to remember only the important stations from Bhandup to Kurla. He uses slice( ) to create a simple list.
let stations=["Bhandup","Kanjur","Vikroli","Ghatkoper","Vidyavihar","Kurla","Chunabhati",
"GTB","Vadala"];
let impStations=stations.slice(0,6);
//Output: ["Bhandup","Kanjur","Vikroli","Ghatkoper","Vidyavihar","Kurla"]
🚇Phase 2: Changing Train at Kurla
4️⃣ splice( ) → Switching from Central to Harbour Line
Rahul gets down at Kurla and moves toward harbour platform from where he wants to take train to Vadala. He remove the Central Line stations from station and replaces with Harbor line stations upto Vadala using splice( ).
let stations=["Bhandup","Kanjur","Vikroli","Ghatkoper","Vidyavihar","Kurla"];
stations.splice(0,6,"Kurla H","Chunabhati","GTB","Vadala");
//Output: [ 'Kurla H', 'Chunabhati', 'GTB', 'Vadala' ]
Now, he is on a new train on the Harbour Line…
5️⃣ unshift() → A New friend Joins from Kurla
At Kurla Harbour, Rahul meets his friend Pooja, who is also travelling to Vadala. This is like using unshift( ) to add an element at beginning
trainCompanion.unShift("Pooja");
//output: ["Pooja","Rahul","Neha"]
Now Rahul, Neha and Pooja continues their journey together….
6️⃣ filter( ) → Avoiding Crowded Stations
Rahul wants to list most crowded stations to be careful while traveling.
let stationCrowd=[
{
station:"Kurla",crowd:90
},
{
station:"Chunabatti",crowd:80
},
{
station:"GTB Nager",crowd:60
},
{
station:"Vadala",crowd:40
},
]
let crowdedStations=stationCrowd.filter(stop=> stop.crowd > 60);
//Output: [{ station: 'Kurla', crowd: 90 },{ station: 'Chunabatti', crowd: 80 }]
Now he knows that he want to extra cautious at Kurla and Chunabhati.
7️⃣ some( ) → Checking less crowded stations
Rahul wonders if any station on his journey is not very crowded. He uses some( ) to check.
let stationCrowd = [
{ station: "Kurla", crowd: 90 },
{ station: "Chunabatti", crowd: 80 },
{ station: "GTB Nager", crowd: 60 },
{ station: "Vadala", crowd: 40 },
];
let crowdedStations = stationCrowd.some(stop => stop.crowd <= 60);
//Output: true(becuase GTB Nager and Vadala has less crowd)
Now he knows GTB Nager and Vadala is less crowded.
8️⃣find( ) → Searching for Vadala
As the train nears Vadala, Rahul wonders if his station is next. He uses find( ) to check.
let station = [
{ station: "Kurla", crowd: 90 },
{ station: "Chunabatti", crowd: 80 },
{ station: "GTB Nager", crowd: 60 },
{ station: "Vadala", crowd: 40 },
];
let destination = station.find(stop => stop.station === "Vadala");
//Output: { station: 'Vadala', crowd: 40 }
9️⃣ shift( ) → Finally all three get’s off
Finally, at Vadala, Pooja get’s off first then Rahul and at last Neha. This is like using shift( ) to remove elements from an array.
let companions=["Pooja","Rahul","Neha"];
companions.shift() //["Rahul","Neha"]
companions.shift()//["Neha"]
companions.shift()//[]
Now they all heads towards their college…
🔟reduce( ) → Calculating the Total Journey Time
Rahul checks how long his entire journey took using reduce( ).
let stationTime=[5,7,6,8,10,9]
let totalTime=stationTime.reduce((total,time)=> total + time,0);
//output: 45
Now, Every Mumbai Local Ride = JavaScript Learning
Next time you change trains at Kurla, remember - you are using JavaScript array method in real Life!