Before getting started, I am going to use arrow function and in case you don't know the arrow functions don't worry, I have used normal functions (ES5 syntax) also!
So Let's get started :
This blog includes →
- For Each
1.) For Each:
This is a simple for loop.
Now let's try this same thing using for Each loop.
For each helps you to reduce the lines of code and makes the code much more readable.
You can also give index in for each loop:
The first parameter is the variable in which all the values are stored one by one, and the second parameter is the index of the current iteration.
As the name suggests, .filter() is a method that helps us to filter some values according to a given condition.
The .filter() method creates a new array with the values which is falling under the given condition from the existing array.
.map() method creates a new array and performs a particular function for each element in the array.
This does not change the original array as it creates a new array.
As the name suggests .sort() method helps us to sort the array according to our condition.
If the condition is true then we return 1 or else we return -1, to get a better idea about it, we can use an example:
If we want to sort the companies according to their start years.
.sort() also makes a new array and the original array is untouched.
Here in this function, we have 2 parameters 'a' and 'b' which will be assigned to 2 values from the array and then they will be compared and then filled in the new array.
.reduce() method reduces the whole array to a single value.
reduce method executes a given function for each value in the array from left to right (starting from index 0 to array's length - 1), for example, we have to find the sum of all the ages, so without for loop we can do this using reduce function.
We have to give 0 to set the initial value from which the values will start although it is optional so you may skip it.
This is a bonus array method for y'all!
This method checks if the entered value is in the array or not.
If the searched value is found, it will return the position/index else it will return -1.
Note: idexOf() is cases sensitive.
Thank you so much for reading the whole blog 🎉, if you liked it do share it with your developer friends, feel free to comment and give suggestions.
I am a Flutter and a Full-Stack Web Developer who loves to write blogs, and play games in my free time.
Kwa mfano, kama unajua jinsi ya kutumia function ya arrow, unaweza kutumia function ya ES5. Kwa hivyo, hebu tuanze: Blogi hii inajumuisha → - Kwa Kila 1.) Kwa ajili ya: Hii ni rahisi kwa ajili ya kitanzi. Sasa hebu kujaribu hii kitu sawa kwa kutumia kwa kila kitanzi. Kwa kila mmoja husaidia kupunguza mistari ya kificho na hufanya kificho zaidi kusoma. Unaweza pia kutoa index katika kwa kila kitanzi: parameter ya kwanza ni variable ambayo maadili yote ni kuhifadhiwa moja kwa moja, na parameter ya pili ni index ya iteration ya sasa. Kama jina linavyopendekeza,.filter() ni njia ambayo inatusaidia kuchuja baadhi ya maadili kulingana na hali fulani. .filter () mbinu inajenga safu mpya na maadili ambayo ni kuanguka chini ya hali iliyotolewa kutoka safu iliyopo. .map () mbinu inajenga safu mpya na hufanya kazi maalum kwa kila kipengele katika safu. Hii haina kubadilisha safu ya awali kama inaunda safu mpya. Kama jina linavyopendekeza.sort () mbinu inatusaidia kupanga safu kulingana na hali yetu. Ikiwa hali ni kweli, basi tunarudi 1 au -1, ili kupata wazo bora, tunaweza kutumia mfano: Ikiwa tunataka kupanga makampuni kulingana na miaka yao ya kuanza. .sort() pia hufanya safu mpya na safu ya awali ni untouched. Katika kazi hii, tuna vigezo viwili 'a' na 'b' ambavyo vitatolewa kwa maadili mawili kutoka safu na kisha zitapimwa na kujazwa katika safu mpya. .reduce() mbinu hupunguza safu nzima kwa thamani moja. "Mfano: ""Reduce"" ni utaratibu wa kuendesha kazi kwa kila thamani katika safu kutoka kushoto kwenda kulia (kuanzia index 0 hadi urefu wa safu - 1) kwa mfano, tunahitaji kupata jumla ya umri wote, hivyo bila kwa for loop tunaweza kufanya hivyo kwa kutumia kazi ya kupunguza." "Kama ""0"" ni thamani ya awali, ambayo inapaswa kuonekana, basi inapaswa kuonekana kama ""0"" na ""0"" ni thamani ya awali, ambayo inapaswa kuonekana kama ""0.""" Hii ni mbinu ya safu ya ziada kwa ajili yenu nyote! Mbinu hii inachunguza kama thamani iliyoingizwa ni katika safu au la. Ikiwa thamani ya utafutaji hupatikana, itarudi nafasi ya index, vinginevyo itarudi -1. Kumbuka: idexOf() ni kesi nyeti. Asante sana kwa kusoma blogu nzima <unk>, kama wewe kama ni kufanya kushiriki na marafiki wako developer, kujisikia huru kutoa maoni na kutoa mapendekezo. Mimi ni Flutter na Full-Stack Web Developer ambaye anapenda kuandika blogu, na kucheza michezo katika muda wangu wa bure.