2016-08-30 14 views
7

Jaki jest najbardziej wydajny i/lub najbardziej czytelny sposób napisania funkcji, która przyjmuje tablicę i zwraca stopień wielowymiarowości tej tablicy. Na razie można założyć, że tablice zawierają tylko typy pierwotne.Znajdź wymiarowość tablicy javascript

Przykład.

var arr = [[1,2],[3,4],[5,6]] 

    function findDim(a){ 
    //logic goes here 
    } 

    findDim(arr); // returns 2 
+1

Co będzie "wymiarowości" z tego: '[1 [2,3], [[4]]] '? – georg

+0

możemy powiedzieć, że ta funkcja powinna obliczyć maksymalną dimencjonalność - czyli 3. – gilmatic

Odpowiedz

9

Zastosowanie rekurencję Array.isArray sposób sprawdzić element jest tablicą.

var arr = [ 
 
    [1, 2], 
 
    [3, 4], 
 
    [5, 6] 
 
]; 
 

 
function findD(arr) { 
 
    // check the element is an array then do 
 
    // recursion to check it's element 
 
    if (Array.isArray(arr)) { 
 
    return 1 + findD(arr[0]); 
 
    } 
 
    // else return `0` since it's not 
 
    // a nested array 
 
    return 0; 
 
} 
 

 
console.log(findD(arr));


FYI: Dla sprawdzenia starsze przeglądarki polyfill option of Array.isArray method.


UPDATE: Okrywać zawiera inną tablicę zwymiarowane i chcesz uzyskać głębszy wymiar następnie użyć Array#map i Math.max metod.

var arr = [ 
 
    [1, 2], 
 
    [3, 4], 
 
    [5, [6]] 
 
]; 
 

 
function findD(arr) { 
 
    // return 0 if not array else return the max value 
 
    // by finding all elements dimension 
 
    return Array.isArray(arr) ? 
 
    // generate the dimension value array 
 
    1 + Math.max.apply(Math, arr.map(findD)) : 0; 
 
} 
 

 
console.log(findD(arr));


Albo z Array#reduce metody, aby uzyskać wartość max.

var arr = [ 
 
    [1, 2], 
 
    [3, [4,[[3]]]], 
 
    [5, [6]] 
 
]; 
 

 
function findD(arr) { 
 
    // return 0 if not array else return the max value 
 
    // by finding all elements dimension 
 
    return Array.isArray(arr) ? 1 + arr.reduce(function(a, b) { 
 
    // get the largest by comparing all the adjuscent 
 
    // elements dimension 
 
    return Math.max(a, findD(b)); 
 
    }, 0) : 0; 
 
} 
 

 
console.log(findD(arr));

+1

Teraz jest znacznie lepiej – Redu

+1

'funkcja (v) {return findD (v)}' nie ma większego sensu. – georg

+0

@georg: oops Właśnie tęskniłem za tym ... dzięki :) –

2

"wymiarowości" nie jest dobrze określony JS macierzy (które nie są niezbędne matryce), tutaj funkcją znaleźć max "głębokości" w tablicy:

maxDepth = x => Array.isArray(x) 
 
    ? 1 + Math.max.apply(this, x.map(maxDepth)) 
 
    : 0 
 
; 
 

 
console.log(maxDepth([[1,2],[3,4],[5,6]])) 
 
console.log(maxDepth([[[[1]]], 2]))

+0

To jest jedyna odpowiedź, która działa (dobrze .. wraz z moją - :)) – Redu

+0

@Redu: cóż, OP nie powiedział jak oni (lub ich nauczyciel) chcą obsługiwać nieregularne tablice ... nie do końca pewni, co jest tutaj "poprawne". – georg

1

Tak właśnie zrobiłbym. Obowiązuje dla nieregularnych tablic wielowymiarowych;

var arr = [[1,2],[3,4],[5,[6,[7,[8]]]]], 
 
findDim = a => Math.max(...a.map(e => Array.isArray(e) ? findDim(e) : 0)) + 1 
 
console.log(findDim(arr))

Powiązane problemy