JSFiddle - React, Tailwind, and code Playground

by Pankaj Kargirwar

JavaScript

// Your expand function
function expand(element) {
  // Your logic here to determine whether to expand or not
  // For example, let's assume it always returns an array
  return [element, element + 1];
}

// Your original array
const originalArray = [1, 2, 3];

// Use map to apply expand to each element and flat to flatten the result
const resultArray = originalArray.map(expand).flat();

console.log(resultArray);
// Output: [1, 2, 2, 3, 3, 4]