Issue
In JavaScript, the Array.map() function exists such that
const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]
I need a bash equivalent where I can take my array, manipulate its contents, then receive a new array with the manipulations.
array1=(1 4 9 16)
map1=# ????
echo ${map1[*]}
Solution
Soooooooo, just write the loop.
array1=(1 4 9 16)
map1=()
for i in "${array1[@]}"; do
map1+=("$((i * 2))")
done
echo "${map1[@]}"
Might be a good time to re-read an introduction to Bash arrays.
Answered By - KamilCuk Answer Checked By - Cary Denson (WPSolving Admin)