How to find the index of an element in a multidimensional array, please write an extremely simple program.
- We go on the outer loop, in which there is an inner loop, on which we also go, going over the indices, and we compare the elements of the array with the required value, if we find equal, we return the index of the outer or inner loop or the object containing both indices and exit the loops. PS You do not care that because of such questions, your reputation tends to -∞? - Specter
- Not really, I'm new, so the main thing for me is to learn. - Irina
|
1 answer
var arr=new Array(); var res=new Array(); for(var i=0;i<=arr.length;i++){ for(var j=0;j<=arr[i].length;j++){ if(el==arr[i][j]){ res[0]=i;res[1]=j; break; } } if(res[0]!=null){ break; } }
- are these records equivalent? var arr = new Array (); var arr = []; guru js correct me. - Specter
- Yes, equivalent. What is the difference how to define it? I do not understand the essence of the comment - stck
- one@stck, for example, in the case of a string, the entry
new String()
and""
not equivalent (to be exact - not really). Although yes, there is no difference in the case of arrays, unless it is customary to use short notation[]
- Zowie - Thanks for the amendment, for the future I will consider. - stck
|