Saturday 25 August 2018

Javascript: Unshift to second position of the array





I understand this diagram:



Unshift/Push diagram



But now my question is... How can I add an element just in the second position of the Array?



If I have this array: (A, C, G, T) and I want to add B...



The result that I want should be: (A, B, C, G, T)




Any suggestions?



Thank you!


Answer



What you want is the splice function:



arr.splice(index, 0, item); will insert item into arr at the specified index.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...