hii,
We can insert mutliple value in an array using different method as per requirements.
Here is the list of method we can use to insert values in an array.
- Array fill() Method :fill() method is used when we want to fills either all the elements in an array with a static value replacing the pre-existing values or values will be filled from the defined index using starting fill and ending fill.
Example:
- In this example all the values in an array will be filled with a static value.
<button onclick="fillAll()">Try it</button>
<p id="fillArray"></p>
<script>
var languages = ["Hindi", "Chinese", "French"];
document.getElementById("fillArray").innerHTML = languages;
function fillAll() {
document.getElementById("fillArray").innerHTML = languages.fill("English");
}
</script>
2. In this example values will be filled from the defined index using starting fill and ending fill with a static value,where starting fill will define the index to start filling the array and ending fill will define the index to stop filling the array.
Syntax:array.fill(value,start,end)
<button onclick="fillValue()">Try it</button>
<p id="fill"></p>
<script>
var languages = ["Hindi", "Chinese", "French"];
document.getElementById("fill").innerHTML = languages;
function fillValue() {
document.getElementById("fill").innerHTML = languages.fill("English,0,2");
}
</script>
Using push() method we can add one or more than one items.
Example: In this example new elements will be added to the an array using push method.
<button onclick="pushData()">Try it</button>
<p id="push"></p>
<script>
var languages = ["Hindi", "Chinese", "French"];
document.getElementById("push").innerHTML = languages;
function pushData() {
languages.push("English", "Arabic", "Spanish");
document.getElementById("push").innerHTML = languages;
}
</script>
Using unshift() method we can add one or more than one items.
Example: In this example new elements will be added to the beginning of an array using unshift() method.
<button onclick="fillBeginning()">Try it</button>
<p id="fillArrayBeginning"></p>
<script>
var languages = ["Hindi", "Chinese", "French"];
document.getElementById("fillArrayBeginning").innerHTML = languages;
function fillBeginning() {
languages.unshift("English", "Spanish");
document.getElementById("fillArrayBeginning").innerHTML = languages;
}
</script>
[Click and drag to move]
Syntax: array.splice(index,no.of items,item1,.....,itemX)
-----> In splice() method three parameter values is required.
-----> Index value is required to identify position of items to be added or removed.
------> No. of items to be removed need to be specified. If no items will be removed set it to 0.
-----> itemX specifies new item(s) that will be added to the array.
Example:
In this example items will be either removed or added to an array.
<button onclick="splice()">Try it</button>
<p id="spliceArray"></p>
<script>
var languages = ["Hindi", "Chinese", "French"];
document.getElementById("spliceArray").innerHTML = languages;
function splice() {
languages.splice(2, 0, "English", "Spanish");
document.getElementById("spliceArray").innerHTML = languages;
}
</script>
0 Comment(s)