1.) flat(): The flat() method creates a new array by concatenating all subarrays within a nested array up to a specified depth.
let newArray = array.flat(depth);
- depth (optional): An integer specifying the depth level of flattening. If not provided, the default depth is 1.
const nestedArray = [1, [2, 3], [4, [5, 6]]]; const flattenedArray = nestedArray.flat(); console.log(flattenedArray); // Output: [1, 2, 3, 4, [5, 6]]
let newArray = array.flatMap(callback);
const numbers = [1, 2, 3, 4, 5]; const doubledArray = numbers.flatMap(num => [num, num * 2]); console.log(doubledArray); // Output: [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]