“Flatmap JavaScript” Kode Jawaban

Flatmap JavaScript

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

arr.flatMap(x => [x, x * 2]);
// is equivalent to
arr.reduce((acc, x) => acc.concat([x, x * 2]), []);
// [1, 2, 2, 4, 3, 6, 4, 8]
Powerful Porpoise

metode flatmap ()

const arr1 = [1, 2, 3, 4, 5];

const newArr1 = arr1.flatMap((x) => [x ** 2]);
console.log(newArr1); // [ 1, 2, 3, 4, 5 ]

// can also be done as
const intermediate = arr1.map((x) => [x ** 2]);
console.log(intermediate); // [ [ 1 ], [ 4 ], [ 9 ], [ 16 ], [ 25 ] ]

const newArr2 = intermediate.flat();
console.log(newArr2); // [ 1, 4, 9, 16, 25 ]

const numbers = [1, 2, 3, 4, 5, 6, 7];

// remove odd and split even element to two half elements
function func(n) {
  if (n % 2 === 0) {
    return [n / 2, n / 2];
  } else {
    return [];
  }
}
const newArr3 = numbers.flatMap(func);
console.log(newArr3); // [ 1, 1, 2, 2, 3, 3 ]
Shibu Sarkar

Jawaban yang mirip dengan “Flatmap JavaScript”

Pertanyaan yang mirip dengan “Flatmap JavaScript”

Lebih banyak jawaban terkait untuk “Flatmap JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya