Indian Politics and JavaScript Array Methods: A Fun Comparison ๐Ÿš€๐Ÿ‡ฎ๐Ÿ‡ณ

Indian Politics and JavaScript Array Methods: A Fun Comparison ๐Ÿš€๐Ÿ‡ฎ๐Ÿ‡ณ

ยท

4 min read

JavaScript Array Methods are incredibly powerful, but learning them can sometimes be boring. ๐Ÿคฏ What if we relate them to Indian politics? It will not only make learning easier but also more fun! ๐ŸŽ‰

Today, weโ€™ll understand 10 JavaScript Array Methods with examples from Indian politics. Letโ€™s begin this exciting journey! ๐Ÿš€

1. push() โ€“ Adding a New Party or Leader

Function: push() adds a new item at the end of an array.
Political Example: When a new leader joins a political party.

let politicalParties = ["BJP", "Congress", "AAP"];
politicalParties.push("TMC"); 

console.log(politicalParties);
// ["BJP", "Congress", "AAP", "TMC"]

๐Ÿ“Œ TMC (Trinamool Congress) has now joined the list of political parties!


2. pop() โ€“ Removing a Leader or Party

Function: pop() removes the last item from an array.
Political Example: When a leader leaves a party.

let politicalParties = ["BJP", "Congress", "AAP", "TMC"];
politicalParties.pop();  

console.log(politicalParties);
// ["BJP", "Congress", "AAP"]

๐Ÿ“Œ TMC has now been removed from this list!


3. shift() โ€“ Removing the Oldest Party or Leader

Function: shift() removes the first item from an array.
Political Example: When an old party dissolves or a veteran leader retires.

let politicalParties = ["Janata Dal", "BJP", "Congress", "AAP"];
politicalParties.shift();  

console.log(politicalParties);
// ["BJP", "Congress", "AAP"]

๐Ÿ“Œ Janata Dal is no longer part of Indian politics!


4. unshift() โ€“ Revival of an Old Alliance

Function: unshift() adds a new item at the beginning of an array.
Political Example: When an old party revives and comes back into the limelight.

let politicalParties = ["BJP", "Congress", "AAP"];
politicalParties.unshift("Shiv Sena");

console.log(politicalParties);
// ["Shiv Sena", "BJP", "Congress", "AAP"]

๐Ÿ“Œ Shiv Sena is back in the spotlight!


5. splice() โ€“ Replacing or Removing a Leader

Function: splice() adds, removes, or replaces elements in an array.
Political Example: When a leader is removed from a party or replaced by another.

let leaders = ["Narendra Modi", "Rahul Gandhi", "Arvind Kejriwal"];
leaders.splice(1, 1, "Amit Shah");

console.log(leaders);
// ["Narendra Modi", "Amit Shah", "Arvind Kejriwal"]

๐Ÿ“Œ Rahul Gandhi is replaced by Amit Shah!


6. slice() โ€“ Forming a New Political Group

Function: slice() extracts a portion of an array and creates a new array.
Political Example: When a faction of a party breaks away and forms a new alliance.

let parties = ["BJP", "Congress", "AAP", "TMC"];
let newAlliance = parties.slice(1, 3); 

console.log(newAlliance);
// ["Congress", "AAP"]

๐Ÿ“Œ Congress and AAP have formed a new coalition!


7. filter() โ€“ Selecting Honest Leaders

Function: filter() selects items from an array based on a condition.
Political Example: Filtering out only non-corrupt leaders.

let leaders = [
  { name: "Narendra Modi", honest: true },
  { name: "Rahul Gandhi", honest: false },
  { name: "Arvind Kejriwal", honest: true }
];

let honestLeaders = leaders.filter(leader => leader.honest);

console.log(honestLeaders);
// [{ name: "Narendra Modi", honest: true }, { name: "Arvind Kejriwal", honest: true }]

๐Ÿ“Œ Only the honest leaders remain in the list!


8. map() โ€“ Changing the Ideology of All Parties

Function: map() applies a transformation to each element in an array.
Political Example: Making every party work for "national interest."

let parties = ["BJP", "Congress", "AAP"];
let transformedParties = parties.map(party => party + " (National Interest)");

console.log(transformedParties);
// ["BJP (National Interest)", "Congress (National Interest)", "AAP (National Interest)"]

๐Ÿ“Œ Now, all parties claim to work for the national interest!


9. reduce() โ€“ Counting Total Seats in an Election

Function: reduce() combines all elements in an array into a single value.
Political Example: Counting the total seats won by all parties in an election.

let seats = [303, 52, 62, 22]; 

let totalSeats = seats.reduce((total, num) => total + num, 0);

console.log(totalSeats);
// 439

๐Ÿ“Œ The total number of seats won by all parties is 439!


Function: find() returns the first element that meets a condition.
Political Example: Finding the leader with more than 100 million followers.

let leaders = [
  { name: "Narendra Modi", followers: 150 },
  { name: "Rahul Gandhi", followers: 30 },
  { name: "Arvind Kejriwal", followers: 20 }
];

let popularLeader = leaders.find(leader => leader.followers > 100);

console.log(popularLeader);
// { name: "Narendra Modi", followers: 150 }

๐Ÿ“Œ Narendra Modi is the most followed leader!

Conclusion

By relating JavaScript Array Methods to Indian politics, we can make learning not only easier but also more entertaining! ๐ŸŽ‰

If you found this article helpful, share it with your friends and fellow developers! ๐Ÿš€

โžก๏ธ Which is your favorite Array Method? Let us know in the comments! ๐Ÿ—ณ๏ธ

ย