JavaScript માં Array Search Methods
JavaScript એરેમાં એલિમેન્ટ્સ શોધવા માટે બિલ્ટ-ઇન (built-in) મેથડ્સ પ્રદાન કરે છે. આ સાધનો તમને પોઝિશન શોધવામાં અથવા કોઈ વેલ્યુ અસ્તિત્વ ધરાવે છે કે નહીં તે તપાસવામાં મદદ કરે છે.
અહીં મુખ્ય મેથડ્સ છે જે તમારે જાણવી જરૂરી છે:
indexOf()કોઈ ચોક્કસ એલિમેન્ટનો પ્રથમ ઇન્ડેક્સ શોધે છે. જો એલિમેન્ટ ન મળે, તો તે -1 રિટર્ન કરે છે. Example:const fruits = ["Apple", "Banana", "Mango", "Banana"];fruits.indexOf("Banana"); // Returns 1lastIndexOf()કોઈ ચોક્કસ એલિમેન્ટનો છેલ્લો ઇન્ડેક્સ શોધે છે. જો એલિમેન્ટ ન મળે, તો તે -1 રિટર્ન કરે છે. Example:const fruits = ["Apple", "Banana", "Mango", "Banana"];fruits.lastIndexOf("Banana"); // Returns 3includes()તમારા એરેમાં કોઈ એલિમેન્ટ અસ્તિત્વ ધરાવે છે કે નહીં તે તપાસે છે. તે true અથવા false રિટર્ન કરે છે. Example:const fruits = ["Apple", "Banana", "Mango"];fruits.includes("Mango"); // Returns truefruits.includes("Orange"); // Returns falsefind()તમારી શરત (condition) સંતોષતું પ્રથમ એલિમેન્ટ રિટર્ન કરે છે. જો કોઈ મેચ ન મળે, તો તેundefinedરિટર્ન કરે છે. Example:const numbers = [5, 12, 8, 20];const result = numbers.find(num => num > 10); // Returns 12findIndex()તમારી શરત સંતોષતા પ્રથમ એલિમેન્ટનો ઇન્ડેક્સ રિટર્ન કરે છે. જો કોઈ મેચ ન મળે, તો તે -1 રિટર્ન કરે છે. Example:const numbers = [5, 12, 8, 20];const index = numbers.findIndex(num => num > 10); // Returns 1findLast()તમારી શરત સંતોષતું છેલ્લું એલિમેન્ટ રિટર્ન કરે છે. તે એરેના અંતથી શોધવાનું શરૂ કરે છે. Example:const numbers = [5, 12, 8, 20];const result = numbers.findLast(num => num > 10); // Returns 20findLastIndex()તમારી શરત સંતોષતા છેલ્લા એલિમેન્ટનો ઇન્ડેક્સ રિટર્ન કરે છે. Example:const numbers = [5, 12, 8, 20];const index = numbers.findLastIndex(num => num > 10); // Returns 3
સારાંશ:
• indexOf() પ્રથમ ઇન્ડેક્સ મેળવે છે.
• lastIndexOf() છેલ્લો ઇન્ડેક્સ મેળવે છે.
• includes() અસ્તિત્વ માટે તપાસ કરે છે.
• find() પ્રથમ મેચિંગ વેલ્યુ મેળવે છે.
• findIndex() પ્રથમ મેચિંગ ઇન્ડેક્સ મેળવે છે.
• findLast() છેલ્લી મેચિંગ વેલ્યુ મેળવે છે.
• findLastIndex() છેલ્લી મેચિંગ ઇન્ડેક્સ મેળવે છે.
Source: https://www.w3schools.com/js/js_array_search.asp
Full post: https://dev.to/kamalesh_ar_6252544786997/array-search-methods-in-javascript-23mk
