Let's explore my fav 10 array methods

Abhishek NemaAbhishek Nema
3 min read

Hanji, so today we will see ten easy-to-understand array methods using some fun analogies (and memes too:)

Disclaimer: This hypothetical example is purely for educational purposes and does not reflect the views, affiliations, or political leanings of any real-life individuals, parties, or reporters. Any resemblance to actual political drama, scandals, or debates is purely coincidental (and probably hilarious). Proceed with a sense of humour and a pinch of satire! 😄

Hypothetically, suppose we have created a New national party list consisting of some great leaders (and some extra great leaders ;) of a country. Now one reporter came and took an interview about our party members, here is the conversation…

reporter: hey AK, could you tell me the respected members of your party?

AK: sure, they are

let partyLeaders = [ "Raah_ul baba", "Keju", "Mota bhai", "56 inch", "Nir_mala tai"];

reporter: great, could you tell me the headcount?

AK: sure, you can easily find it using length method.

let head_count = partyLeaders.length;
console.log(head_count);
// output : 5

reporter: good, but can we start the names from the last to the first, I mean reverse list?

AK: sure, here you go using reverse() method.

let reverse_list = partyLeaders.reverse();
console.log(reverse_list);
//output: [ 'Nir_mala tai', '56 inch', 'Mota bhai', 'Keju', 'Raah_ul baba' ]

reporter: can we remove last name from list?

AK: ahh yes, using pop() method.

let person_out = partyLeaders.pop();
console.log(partyLeaders);
//output: [ 'Nir_mala tai', '56 inch', 'Mota bhai', 'Keju' ]
//"Raah_ul baba" nikal gye ;)

reporter: Hmm I think we can add them, no issues.

AK: okay boss, just use push() method

partyLeaders.push(Lastperson_out); //person_out has "Raah_ul baba" value
console.log(partyLeaders);
//output: [ 'Nir_mala tai', '56 inch', 'Mota bhai', 'Keju', 'Raah_ul baba' ]

reporter: just like last one, can we remove first person also?

AK: yes ji, just use shift() method

let firstPerson_out = partyLeaders.shift(); // "Nir_mala tai" is out
console.log(partyLeaders);
// [ '56 inch', 'Mota bhai', 'Keju', 'Raah_ul baba' ]

reporter: wait wait, I think we should add her, kahin GST badha diya to 👀

AK: hmm, shi baat hai. use unshift() method.

let firstPerson_in = partyLeaders.unshift("Nir_mala tai");
console.log(partyLeaders);
//output: [ 'Nir_mala tai', '56 inch', 'Mota bhai', 'Keju', 'Raah_ul baba' ]

reporter: could you check if “Mota bhai“ is a part of list or not?

AK: sure, includes() method try karo.

let isInList = partyLeaders.includes("Mota bhai");
console.log(isInList);
//output : true

(me in the background: “agar nhi honge to Buldozer chal jaayega bhai 😶‍🌫️“)

reporter: any plans for future?

AK: yes, I have list of some other leaders, we may add them in our party soon using concat() method

let newFaces = ["Thoko taali", "Kumar bahutVisvas", "Laalo Yadav"];
let mixList = partyLeaders.concat(newFaces);
console.log(mixList);
//output : [ 'Nir_mala tai', '56 inch', 'Mota bhai', 'Keju', 'Raah_ul baba', 'Thoko taali', 'Kumar bahutVisvas', 'Laalo Yadav'
]

reporter: is there any way to find Mota bhai’s position?

AK: Hanji, you can use indexOf() method for it. counting will start from zero, not one.

let leader_position = mixList.indexOf("Mota bhai");
console.log(leader_position);
// output: 2

reporter: by talking to you I have one curiosity, suppose I have a nested array like this [1, 2, [3, 4], [true, false]], [“abc”, [111, 222], “def”]], can we convert it to a simple single array?

AK: yes we can use flat(Infinity) method, where Infinity goes till last nested array.

let complexArray = [1, 2, [3, 4], [true, false], ["abc", [111, 222], "def"]];
console.log(complexArray.flat(Infinity));
// output: [1, 2, 3, 4, true, false,'abc', 111, 222,'def']

4
Subscribe to my newsletter

Read articles from Abhishek Nema directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhishek Nema
Abhishek Nema