“JavaScript FLATTEN ARRAY OF ARRAY” Kode Jawaban

JavaScript meratakan array

let arr = [
    [1, 2],
    [3, 4],
    [5, 6][7, 8, 9],
    [10, 11, 12, 13, 14, 15]
];
let flatArray = arr.reduce((acc, curVal) => {
    return acc.concat(curVal)
}, []);

//Output:  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
Elated Earthworm

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 “JavaScript FLATTEN ARRAY OF ARRAY”

Pertanyaan yang mirip dengan “JavaScript FLATTEN ARRAY OF ARRAY”

Lebih banyak jawaban terkait untuk “JavaScript FLATTEN ARRAY OF ARRAY” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya