Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
¿Cómo verificar que la matriz tenga cuántas dimensiones en javascript?

Por ejemplo, tengo una matriz de la siguiente manera y se da el resultado esperado.

En javascript, ¿cómo podemos determinar dinámicamente cuántos niveles hay en mi matriz ary ?

 var ary = ["a","b",["c","d"],"e",["f","g",["h","i"],"j"]]; Output: 3 var ary = ["a","b",["c","d"],"e",["f","g","i"]]; Output: 2 var ary = ["a","b",["c",["d"]],"e",[["f","g",["i","j"]],"k"]]; Output: 4
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aquí hay una función recursiva que atravesará las profundidades de la matriz y rastreará el máximo de ella. Tenga en cuenta que el seguimiento se realiza a través de propiedades adjuntas a la función en sí.

 var ary1 = ["a","b",["c",["d"]],"e",[["f","g",["i","j"]],"k"]]; function getDimension(arr, start) { //Attach depth tracking properties to the function if (start){ getDimension.depth = 0; getDimension.maxDepth = 0; } //Track max depth getDimension.depth++ if (getDimension.depth > getDimension.maxDepth) getDimension.maxDepth++; //Manage recursion for (let element of arr) if (element instanceof Array) getDimension(element); getDimension.depth--; //In first level at this point if (getDimension.depth === 0) return getDimension.maxDepth; } let d = getDimension(ary1, true); console.log(d);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!