“Flatten 2d Array JavaScript” Código de respuesta

Flatten 2d Array JavaScript

const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat());
// expected output: [0, 1, 2, 3, 4]

// Flatten 2 levels deep
const arr2 = [0, 1, 2, [[[3, 4]]]]; 
console.log(arr2.flat(2));
// expected output: [0, 1, 2, [3, 4]]

// Flatten all levels
const arr4 = [2, 2, 5, [5, [5, [6]], 7]];
arr4.flat(Infinity);
// [2, 2, 5, 5, 5, 6, 7];
chinn fang

aplanar una matriz javascript

var arrays = [
  ["$6"],
  ["$12"],
  ["$25"],
  ["$25"],
  ["$18"],
  ["$22"],
  ["$10"]
];
var merged = [].concat.apply([], arrays);

console.log(merged);
Bad Bison

JavaScript aplanan la matriz de matrices

var multiDimensionArray = [["a"],["b","c"],["d"]]; //array of arrays
var flatArray = Array.prototype.concat.apply([], multiDimensionArray); //flatten array of arrays
console.log(flatArray); // [ "a","b","c","d"];
Grepper

la matriz aplanar

const arr = [1, 2, [3, 4]];

// To flat single level array
arr.flat();
// is equivalent to
arr.reduce((acc, val) => acc.concat(val), []);
// [1, 2, 3, 4]

// or with decomposition syntax
const flattened = arr => [].concat(...arr);
Poised Puffin

Respuestas similares a “Flatten 2d Array JavaScript”

Preguntas similares a “Flatten 2d Array JavaScript”

Más respuestas relacionadas con “Flatten 2d Array JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código