Bu eğitimde, örnekler yardımıyla JavaScript çok boyutlu dizileri hakkında bilgi edineceksiniz.
Çok boyutlu bir dizi, başka bir dizi içeren bir dizidir. Örneğin,
// multidimensional array const data = ((1, 2, 3), (1, 3, 4), (4, 5, 6));
Çok Boyutlu Bir Dizi Oluşturun
JavaScript'te çok boyutlu dizileri nasıl oluşturabileceğiniz aşağıda açıklanmıştır.
örnek 1
let studentsData = (('Jack', 24), ('Sara', 23), ('Peter', 24));
Örnek 2
let student1 = ('Jack', 24); let student2 = ('Sara', 23); let student3 = ('Peter', 24); // multidimensional array let studentsData = (student1, student2, student3);
Burada hem örnek 1 hem de örnek 2, aynı verilere sahip çok boyutlu bir dizi oluşturur.
Bir Dizinin Öğelerine Erişim
İndisleri (0, 1, 2…) kullanarak çok boyutlu bir dizinin elemanlarına erişebilirsiniz . Örneğin,
let x = ( ('Jack', 24), ('Sara', 23), ('Peter', 24) ); // access the first item console.log(x(0)); // ("Jack", 24) // access the first item of the first inner array console.log(x(0)(0)); // Jack // access the second item of the third inner array console.log(x(2)(1)); // 24
Çok boyutlu bir diziyi (bu durumda, x) 3 satırlı ve 2 sütunlu bir tablo olarak düşünebilirsiniz.

Çok Boyutlu Bir Diziye Eleman Ekleme
Çok boyutlu bir diziye öğe eklemek için Array'in push () yöntemini veya bir indeksleme gösterimini kullanabilirsiniz.
Dış Diziye Öğe Ekleme
let studentsData = (('Jack', 24), ('Sara', 23),); studentsData.push(('Peter', 24)); console.log(studentsData); //(("Jack", 24), ("Sara", 23), ("Peter", 24)
İç Diziye Eleman Ekleme
// using index notation let studentsData = (('Jack', 24), ('Sara', 23),); studentsData(1)(2) = 'hello'; console.log(studentsData); // (("Jack", 24), ("Sara", 23, "hello"))
// using push() let studentsData = (('Jack', 24), ('Sara', 23),); studentsData(1).push('hello'); console.log(studentsData); // (("Jack", 24), ("Sara", 23, "hello"))
Belirli bir dizine bir öğe eklemek için Array'in splice () yöntemini de kullanabilirsiniz. Örneğin,
let studentsData = (('Jack', 24), ('Sara', 23),); // adding element at 1 index studentsData.splice(1, 0, ('Peter', 24)); console.log(studentsData); // (("Jack", 24), ("Peter", 24), ("Sara", 23))
Çok Boyutlu Bir Diziden Bir Öğeyi Kaldırma
Çok boyutlu bir diziden öğeyi kaldırmak için Array'ın pop () yöntemini kullanabilirsiniz. Örneğin,
Dış Diziden Öğeyi Kaldır
// remove the array element from outer array let studentsData = (('Jack', 24), ('Sara', 23),); studentsData.pop(); console.log(studentsData); // (("Jack", 24))
İç Diziden Öğeyi Kaldır
// remove the element from the inner array let studentsData = (('Jack', 24), ('Sara', 23)); studentsData(1).pop(); console.log(studentsData); // (("Jack", 24), ("Sara"))
splice()
Yöntemi, belirli bir dizindeki bir öğeyi kaldırmak için de kullanabilirsiniz . Örneğin,
let studentsData = (('Jack', 24), ('Sara', 23),); // removing 1 index array item studentsData.splice(1,1); console.log(studentsData); // (("Jack", 24))
Çok Boyutlu Dizi Üzerinde Yineleme
Çok boyutlu dizi üzerinde yineleme yapmak için Array'in forEach () yöntemini kullanarak çok boyutlu bir dizi üzerinde yineleme yapabilirsiniz. Örneğin,
let studentsData = (('Jack', 24), ('Sara', 23),); // iterating over the studentsData studentsData.forEach((student) => ( student.forEach((data) => ( console.log(data); )); ));
Çıktı
Jack 24 Sara 23
İlk forEach()
yöntem, dış dizi öğeleri forEach()
üzerinde yineleme yapmak için kullanılır ve ikincisi , iç dizi öğeleri üzerinde yineleme yapmak için kullanılır.
for… of
Döngüyü çok boyutlu dizi üzerinde yinelemek için de kullanabilirsiniz . Örneğin,
let studentsData = (('Jack', 24), ('Sara', 23),); for (let i of studentsData) ( for (let j of i) ( console.log(j); ) )
Çok boyutlu bir dizi üzerinde yineleme yapmak için for döngüsünü de kullanabilirsiniz. Örneğin,
let studentsData = (('Jack', 24), ('Sara', 23),); // looping outer array elements for(let i = 0; i < studentsData.length; i++)( // get the length of the inner array elements let innerArrayLength = studentsData(i).length; // looping inner array elements for(let j = 0; j < innerArrayLength; j++) ( console.log(studentsData(i)(j)); ) )