const arr = [2,3,4,5];
allData = arr[1].concat(9);
In order to add an item into the middle of an array you can use the .splie()
function. You can achieve your desired affect by using the code:
const arr = [2,3,4,5]; arr.splice(1, 0, 9) console.log(arr)
To understand the use of .splice()
. The first param is the starting point, so where we are entering the item into, the second param is how many items will be deleted and finally the last param is what is going to be inserted into the array.
Thanks! Hope this helps!
You can use method SPLICE for arrays. Splice is a powerful method to remove or add at any position an element in an array.
const arr = [2,3,4,5];
ADD AN ELEMENT
add 9 as second element in our array
arr.splice(1, 0, 9)
console.log(array) = [2,9,3,4,5]
REMOVE AN ELEMENT
const arr = [2,3,4,5];
remove the second element(3) and insert 9 instead
arr.splice(1, 1, 9)
console.log(arr) = [2,9,4,5]
I hope this helps you understand the method better.
You can an item in between array using .splice() method.
For further information regarding this you can check this link. [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice][1]
Hope this helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.