简体   繁体   English

如何获取数组中数组的长度?

[英]How to get the length of an array inside an array?

I need to get length of an array inside an array for my iteration.我需要在数组中获取数组的长度以进行迭代。

I have three arrays that contain names from three different majors in my college that nested in one array called 'jurusan' that means major in English.我有三个数组,其中包含来自我大学三个不同专业的名称,它们嵌套在一个名为“jurusan”的数组中,意思是英语专业。

and I want to put all that names into three new groups that contain names from three different majors and put it inside each group.我想将所有这些名称放入三个新组中,其中包含来自三个不同专业的名称,并将其放入每个组中。

How can I access the length of the Array contained inside jurusan?如何访问包含在 jurusan 中的数组的长度? my code reads the length of jurusan which is 3, it should get the length of tif array which is 5 and so on.我的代码读取 jurusan 的长度为 3,它应该得到 tif 数组的长度为 5,依此类推。

 var tif = ['idris', 'akbar', 'adyusman', 'a', 'g']; var te = ['tolaal', 'badri', 'alaina', 'b']; var mt = ['ressi', 'arian', 'tifa', 'c']; var jurusan = [tif, te, mt]; var kel = [ [], [], [] ]; for (i = 0; i < jurusan.length; i++) { for (j = 0; j < jurusan[i][j].length; j++) { kel[j].push(jurusan[i][j]); } } for (i = 0; i < kel.length; i++) { console.log(kel[i]) } document.getElementById("a").innerHTML = kel[0] + '<br>' + kel[1] + '<br>' + kel[2];
 <p id="a"></p>

How can I access the length of the Array contained inside jurusan?如何访问包含在 jurusan 中的数组的长度?

It seems pretty straight to me:这对我来说似乎很直接:

 var tif = ['idris','akbar','adyusman','a','g']; var te = ['tolaal','badri','alaina','b']; var mt = ['ressi','arian','tifa','c']; var jurusan= [tif,te,mt]; // with forEach() jurusan.forEach(n => console.log(n.length)); // with index console.log(jurusan[0].length); console.log(jurusan[1].length); console.log(jurusan[2].length);

const input = [ ['a', 'b', 'c', 's', '1s'], ['c', 'd', 'f'], ['d', 'f', 'g', 'h'], ];常量输入 = [ ['a', 'b', 'c', 's', '1s'], ['c', 'd', 'f'], ['d', 'f', ' g', 'h'], ]; const arrayOfLength = (prop) => { let length = prop.map(num => num.length) console.log(length) } arrayOfLength(input) const arrayOfLength = (prop) => { let length = prop.map(num => num.length) console.log(length) } arrayOfLength(input)

// expected output [5, 3, 4] // 预期输出 [5, 3, 4]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM