2016-06-22 8 views
-1

hier abgeflacht ich ein zweidimensionales Array wie folgtFlatten n-dimensionalen Array ohne concat oder reduzieren

function flatArray(a, b) { 
 

 
    return a.concat(b); 
 

 
} 
 

 
console.log([ 
 
    ['1', '2', '3'], 
 
    ['4', '5', '6'] 
 
].reduce(flatArray));

Kann ich diese ohne verringere, und concat? Ich kann nicht

+0

Sicher, Sie Schleife durch die Arrays und sie kombinieren. – ssube

+0

Ich habe eine Lösung, um var array = [[1,2,3], [4,5,6]]; in ein anderes neues leeres Array zu reduzieren. Aber ich bin mir nicht sicher, wie man das ursprüngliche Array flach hält, ohne es in ein anderes –

Antwort

0

Hier ist eine relativ saubere Lösung:

function flatten(ar) { 
 
    // if ar is not an array, return it in a single-element array 
 
    if(!Array.isArray(ar)) { return [ar]; } 
 

 
    // convert each element of ar to a flattened copy of itself 
 
    var flatEls = ar.map(flatten); 
 

 
    // create a new array 
 
    var newArr = []; 
 

 
    // push the contents of each array in flatEls into the new array 
 
    flatEls.forEach(function(el) { newArr.push.apply(newArr, el); }); 
 

 
    // return the new array 
 
    return newArr; 
 
} 
 

 
console.log(flatten([['1', '2', '3'], ['4', '5', ['6'], ['7', '8']]]));

Und ohne die Kommentare:

function flatten(ar) { 
 
    if(!Array.isArray(ar)) { return [ar]; } 
 

 
    var flatEls = ar.map(flatten); 
 

 
    var newArr = []; 
 

 
    flatEls.forEach(function(el) { newArr.push.apply(newArr, el); }); 
 

 
    return newArr; 
 
} 
 

 
console.log(flatten([['1', '2', '3'], ['4', '5', ['6'], ['7', '8']]]));

+0

Könnten Sie das erklären? und: Syntax. Ich habe sie nachgeschlagen, bin aber verwirrt über die Verwendung hier –

+0

Es ist der [Bedingungsoperator] (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator). Sie sehen 'return Array.isArray (el)? flatten (el): [el]; 'als Kurzform für' if (Array.isArray (el)) {return flatten (el); } else {zurück [el]; } ' – JLRishe

0

Die einfachste Art und Weise einen Weg finden, dies zu tun wäre:

function flatten2D(arr) { 
    var flatArr = []; 
    for (var i = 0; i < arr.length; i++) { 
    for (var j = 0; j < arr[i].length; j++) { 
     flatArr.push(arr[i][j]) 
    } 
    } 

    return flatArr 
} 

Denken Sie daran, das jedes Element in der äußeren Anordnung annimmt, ist ein Array. Zusätzliche Typprüfungen können bei Bedarf zu der Schleife hinzugefügt werden.

+0

einzubringen Könnten Sie meine Lösung betrachten und sehen, wie ich versuchen kann, es zu beheben? –

0

Sie können Rekursion durch die Vermeidung von mit leichter Variation als @NinaScholz Lösung so etwas tun.

function flatten(a) { 
 
    // iterate over array elements 
 
    for (var i = 0,ar; i < a.length; i++) { 
 
    // check the current element is array 
 
    if (Array.isArray(a[i])) { 
 
     // copy the array 
 
     ar = a[i].slice(); 
 
     // push value at the beginning 
 
     //for passing ass splice arg, 1 for delete count 
 
     ar.unshift(1); 
 
     // push value at the beginning 
 
     // for passing ass splice arg, i for the index 
 
     ar.unshift(i); 
 
     // call splice where a as this arg and 
 
     // copied array as param - which delete the 
 
     // element and add the array element to parent 
 
     [].splice.apply(a, ar);  
 
     // decrement the value since we are extracted 
 
     // the array, otherwise it will skip the first 
 
     // element from the extracted array 
 
     i--; 
 
    } 
 
    } 
 
} 
 

 
var array = [ 
 
    [['1'], '2', '3'], 
 
    ['4', '5', ['6'], 
 
    ['7', '8'] 
 
    ] 
 
]; 
 

 
flatten(array); 
 

 
console.log(array);

1

Ein anderer Ansatz, arbeitet in situ.

function flatten(array) { 
 
    var l = array.length, temp; 
 
    while (l--) { 
 
     if (Array.isArray(array[l])) { 
 
      flatten(array[l]); 
 
      temp = array[l].slice(); 
 
      temp.unshift(1); 
 
      temp.unshift(l); 
 
      [].splice.apply(array, temp); 
 
     } 
 
    } 
 
} 
 

 

 
var array = [['1', '2', '3'], ['4', '5', ['6'], ['7', '8']]]; 
 

 
flatten(array); 
 

 
console.log(array);

+0

sein [magic] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply)! :-) –

+0

@cresjoy: das ist für das Splice-Methode-Argument .... das entfernt ein Element bei Index 'l' und fügt den Rest des Elements dort hinzu –

0

Dieses recht funktioniert glücklich in n Dimensionen, über Rekursion.

function appendToArray (array, items) { 
    var item; 
    var i = 0; 
    var length = items.length; 

    for (; i < length; i += 1) { 
    item = items[i]; 
    if (Array.isArray(item)) { 
     appendToArray(array, item); 
    } else { 
     array.push(item); 
    } 
    } 

    return array; 
} 

function flatten (input) { 
    return appendToArray([], input); 
} 


flatten([1, 2, 3, [4, 5, [6, [7]]], [8, 9]]); 
0

http://pastebin.com/eu7Jw3X0

// return two-dimension array 
function filler (dimension, from) { 

    var content = [], i = 0, j = 0, dimension = dimension || 0; 

    while ((i = content.push([])) < dimension) 
     while ((j = content[ (i - 1) ].push(reflect(i, j, from))) < dimension); 
    while ((j = content[ (i - 1) ].push(reflect(i, j, from))) < dimension); 

    content.unshift(content.pop()); 

    function reflect(i, j, from) { 
     if (!from) 
      return i == j ? 1 : 0; 
     else 
      return from.pop(); 
    }; 
};