2
Suppose I have the following array:
const arr = [1, 2, 4, 5, 7]
If I want to add the number 3 at position 02 of the array I would do as follows:
const add = arr.splice(2,0, 3)
console.log(arr)
The question is: What is the best way to add also number 6 at position 4 of the array in this same const add
The "number 6 at position 4" is not really a "random element", as commented in the title. What I meant by "random elements"?
– Woss
It is worth remembering that
splice
returns an array with the deleted elements. But as you pass zero in the second parameter, no element is deleted, soadd
will always be an empty array (that is, in this code snippet this variable seems unnecessary - unless, of course, the code is in a larger context in which the return is relevant, then it makes sense to store it in a variable)– hkotsubo