Is there a shorter way of doing this:
let arrSlicePush = arr.slice();
arrSlicePush.push(num);
let x = func(arrSlicePush);
Thanks in advance.
You would call Array.prototype.concat instead of Array.prototype.push.
Also, since concat already merges two arrays and returns a new one, you so not even need to slice the original array.
const arr = [ 0, 1, 2, 3, 4 ];
const num = 5;
const func = (arr) => arr.map(e => String.fromCharCode(e + 65));
const x = func(arr.concat(num));
console.log(x);
[ ...arr, val ] is syntactic sugar for arr.concat(val)
This works:
func(animals.slice().concat(num));
.slice() ;)
let ArrSlicePush = Arr.slice(); ArrSlicePush.push(Num); let x = Func(ArrSlicePush);?.push()returns number of elements pushed+original length of array. So, you made a mistake. You didn't write it shorter.