“Ratakan JavaScript Array 2D” Kode Jawaban

Ratakan JavaScript Array 2D

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

Ratakan javascript array

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

console.log(merged);
Bad Bison

JavaScript FLATTEN ARRAY OF ARRAY

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 rata

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

Jawaban yang mirip dengan “Ratakan JavaScript Array 2D”

Pertanyaan yang mirip dengan “Ratakan JavaScript Array 2D”

Lebih banyak jawaban terkait untuk “Ratakan JavaScript Array 2D” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya