“2D -Array JavaScript abflachen” Code-Antworten

2D -Array JavaScript abflachen

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

ein Array JavaScript flachen

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

console.log(merged);
Bad Bison

JavaScript Flacharray von Arrays

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

Array flach

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

Ähnliche Antworten wie “2D -Array JavaScript abflachen”

Fragen ähnlich wie “2D -Array JavaScript abflachen”

Weitere verwandte Antworten zu “2D -Array JavaScript abflachen” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen