जावास्क्रिप्ट सरणी (उदाहरण के साथ)

इस ट्यूटोरियल में, आप उदाहरणों की मदद से जावास्क्रिप्ट सरणी के बारे में जानेंगे।

जैसा कि आप जानते हैं, एक चर एक तत्व को संग्रहीत कर सकता है। यदि आपको एक साथ कई तत्वों को संग्रहीत करने की आवश्यकता है, तो आप एक सरणी का उपयोग कर सकते हैं।

एक सरणी एक वस्तु है जो कई तत्वों को संग्रहीत कर सकती है । उदाहरण के लिए,

 const myArray = ('hello', 'world', 'welcome');

एक सरणी बनाएँ

आप दो तरीकों का उपयोग करके एक सरणी बना सकते हैं:

1. एक सरणी शाब्दिक का उपयोग करना

एक सरणी बनाने का सबसे आसान तरीका एक सरणी शाब्दिक का उपयोग करके है ()। उदाहरण के लिए,

 const array1 = ("eat", "sleep");

2. नए कीवर्ड का उपयोग करना

आप जावास्क्रिप्ट के newकीवर्ड का उपयोग करके एक सरणी भी बना सकते हैं ।

 const array2 = new Array("eat", "sleep");

उपरोक्त दोनों उदाहरणों में, हमने दो तत्वों वाले एक सरणी बनाया है।

नोट : सरणी बनाने के लिए सरणी शाब्दिक का उपयोग करने की अनुशंसा की जाती है।

यहां सरणियों के और उदाहरण दिए गए हैं:

 // empty array const myList = ( ); // array containing number values const numberArray = ( 2, 4, 6, 8); // array containing string values const stringArray = ( 'eat', 'work', 'sleep'); // multiple data types array const newData = ('work', 'exercise', 1, true);

आप ऐरे के अंदर एरेज़, फ़ंक्शंस और अन्य ऑब्जेक्ट भी स्टोर कर सकते हैं। उदाहरण के लिए,

 const newData = ( ('task1': 'exercise'), (1, 2 ,3), function hello() ( console.log('hello')) );

ऐरे के तत्वों तक पहुँच

आप सूचक (0, 1, 2…) का उपयोग करके किसी सरणी के अंदर तत्वों तक पहुँच सकते हैं । उदाहरण के लिए,

 const myArray = ('h', 'e', 'l', 'l', 'o'); // first element console.log(myArray(0)); // "h" // second element console.log(myArray(1)); // "e"
जावास्क्रिप्ट में ऐरे इंडेक्सिंग

नोट : एरे का सूचकांक 0 से शुरू होता है, 1 से नहीं।

एक तत्व को एक सरणी में जोड़ें

आप अंतर्निहित पद्धति का उपयोग कर सकते हैं push()और unshift()एक सरणी में एक तत्व जोड़ सकते हैं।

push()विधि एक सरणी के अंत में एक तत्व को जोड़ता है और एक नई सरणी की लंबाई देता है। उदाहरण के लिए,

 let dailyActivities = ('eat', 'sleep'); // add an element at the end of the array dailyActivities.push('exercise'); console.log(dailyActivities); // ('eat', 'sleep', 'exercise')

unshift()विधि एक सरणी की शुरुआत करने के लिए एक नए तत्व कहते हैं और एक सरणी के नए लंबाई देता है। उदाहरण के लिए,

 let dailyActivities = ('eat', 'sleep'); //add an element at the end of the array dailyActivities.unshift('work'); console.log(dailyActivities); // ('work', 'eat', 'sleep', 'exercise'')

एक ऐरे के तत्वों को बदलें

आप इंडेक्स वैल्यू तक पहुँचकर तत्वों को जोड़ सकते हैं या तत्वों को बदल सकते हैं।

 let dailyActivities = ( 'eat', 'sleep'); // this will add the new element 'exercise' at the 2 index dailyActivities(2) = 'exercise'; console.log(dailyActivities); // ('eat', 'sleep', 'exercise')

मान लीजिए, एक सरणी में दो तत्व हैं। यदि आप सूचकांक 3 (चौथे तत्व) में एक तत्व जोड़ने की कोशिश करते हैं, तो तीसरा तत्व अपरिभाषित होगा। उदाहरण के लिए,

 let dailyActivities = ( 'eat', 'sleep'); // this will add the new element 'exercise' at the 3 index dailyActivities(3) = 'exercise'; console.log(dailyActivities); // ("eat", "sleep", undefined, "exercise")

मूल रूप से, यदि आप उच्च सूचकांकों में तत्वों को जोड़ने का प्रयास करते हैं, तो बीच के सूचकांकों का अपरिभाषित मूल्य होगा।

एक एरे से एक तत्व निकालें

आप pop()किसी सरणी से अंतिम तत्व को निकालने के लिए विधि का उपयोग कर सकते हैं । pop()विधि को भी वापस आ मान देता है। उदाहरण के लिए,

 let dailyActivities = ('work', 'eat', 'sleep', 'exercise'); // remove the last element dailyActivities.pop(); console.log(dailyActivities); // ('work', 'eat', 'sleep') // remove the last element from ('work', 'eat', 'sleep') const removedElement = dailyActivities.pop(); //get removed element console.log(removedElement); // 'sleep' console.log(dailyActivities); // ('work', 'eat')

यदि आपको पहले तत्व को निकालने की आवश्यकता है, तो आप shift()विधि का उपयोग कर सकते हैं । shift()विधि पहला तत्व निकाल देता है और निकाले गए तत्व देता है। उदाहरण के लिए,

 let dailyActivities = ('work', 'eat', 'sleep'); // remove the first element dailyActivities.shift(); console.log(dailyActivities); // ('eat', 'sleep')

अर्रे लंबाई

आप lengthसंपत्ति का उपयोग करके एक तत्व की लंबाई (किसी सरणी में तत्वों की संख्या) पा सकते हैं। उदाहरण के लिए,

 const dailyActivities = ( 'eat', 'sleep'); // this gives the total number of elements in an array console.log(dailyActivities.length); // 2

ऐरे तरीके

जावास्क्रिप्ट में, विभिन्न सरणी विधियाँ उपलब्ध हैं जो उपयोगी गणना करना आसान बनाती हैं।

सामान्यतः इस्तेमाल की जाने वाली जावास्क्रिप्ट सरणी विधियों में से कुछ हैं:

तरीका विवरण
समतल () दो या अधिक सरणियों में शामिल होता है और एक परिणाम देता है
के सूचकांक() एक सरणी के एक तत्व को खोजता है और अपनी स्थिति देता है
खोजें () एक सरणी तत्व का पहला मान लौटाता है जो एक परीक्षण पास करता है
findIndex () एक सरणी तत्व का पहला सूचकांक लौटाता है जो एक परीक्षण पास करता है
प्रत्येक के लिए() प्रत्येक तत्व के लिए एक फ़ंक्शन कॉल करता है
शामिल हैं () जाँचता है कि किसी सरणी में एक निर्दिष्ट तत्व है या नहीं
धक्का दें() किसी सरणी के अंत में एक नया तत्व aads और एक सरणी की नई लंबाई देता है
अनशिफ्ट () adds a new element to the beginning of an array and returns the new length of an array
pop() removes the last element of an array and returns the removed element
shift() removes the first element of an array and returns the removed element
sort() sorts the elements alphabetically in strings and in ascending order
slice() selects the part of an array and returns the new array
splice() removes or replaces existing elements and/or adds new elements

Example: JavaScript Array Methods

 let dailyActivities = ('sleep', 'work', 'exercise') let newRoutine = ('eat'); // sorting elements in the alphabetical order dailyActivities.sort(); console.log(dailyActivities); // ('exercise', 'sleep', 'work') //finding the index position of string const position = dailyActivities.indexOf('work'); console.log(position); // 2 // slicing the array elements const newDailyActivities = dailyActivities.slice(1); console.log(newDailyActivities); // ( 'sleep', 'work') // concatenating two arrays const routine = dailyActivities.concat(newRoutine); console.log(routine); // ("exercise", "sleep", "work", "eat")

Note: If the element is not in an array, indexOf() gives -1.

Visit JavaScript Array Methods to learn more.

Working of JavaScript Arrays

In JavaScript, an array is an object. And, the indices of arrays are objects keys.

चूंकि सरणियाँ ऑब्जेक्ट्स हैं, सरणी तत्वों को संदर्भ द्वारा संग्रहीत किया जाता है। इसलिए, जब एक सरणी मान की प्रतिलिपि बनाई जाती है, तो कॉपी किए गए सरणी में कोई भी परिवर्तन मूल सरणी में भी दिखाई देगा। उदाहरण के लिए,

 let arr = ('h', 'e'); let arr1 = arr; arr1.push('l'); console.log(arr); // ("h", "e", "l") console.log(arr1); // ("h", "e", "l")

आप किसी सरणी में नामित कुंजी को पास करके मानों को संग्रहीत भी कर सकते हैं। उदाहरण के लिए,

 let arr = ('h', 'e'); arr.name = 'John'; console.log(arr); // ("h", "e") console.log(arr.name); // "John" console.log(arr('name')); // "John"
जावास्क्रिप्ट में ऐरे इंडेक्सिंग

हालांकि, किसी सरणी में मनमाने नाम पास करके मूल्यों को संग्रहीत करने की अनुशंसा नहीं की जाती है।

इसलिए जावास्क्रिप्ट में, आपको एक सरणी का उपयोग करना चाहिए, यदि मूल्य ऑर्डर किए गए संग्रह में हैं। अन्यथा इसके साथ ऑब्जेक्ट का उपयोग करना बेहतर है ( )

अनुशंसित लेख

  • जावास्क्रिप्ट forEach
  • जावास्क्रिप्ट के लिए…
  • जावास्क्रिप्ट बहुआयामी सरणी

दिलचस्प लेख...