How to insert an element at a specific index with dojox
What is Dojo?
The Dojo is a JavaScript toolkit (library) that provides many utility modules and functions to create a web application. We can save time and speed up the development process using the predefined Dojo modules. Dojo includes language utilities, UI components, and more.
Insert an element at a specific index
We can use the insert method of the dojox.collections.ArrayList module to insert an element at a specific index of the ArrayList.
Syntax
insert(index, value);
This method takes two arguments
index: This is the index to which the new element is to be inserted.value: This is the value to be inserted.
Code example
Let's look at the code below:
Code explanation
In the above code snippet:
Line 6: We load the Dojo library from the CDN server.
Line 8: We load the
dojox/collections/ArrayListmodule.Line 9: After the
ArrayListclass is loaded, we create a newArrayListobject with value[2,3,4].Line 12: We use the
insertmethod to insert the value0and 0th index of theArrayList. After insertion, the list will be[0,2,3,4].Line 15: We use the
insertmethod to insert the value1at 1st index of theArrayList. After insertion, the list will be[0,1,2,3,4].
Free Resources