Push Value in Empty Index of Array in JavaScript



To push value in an empty index in an array, we must write an array function pushAtEmpty() that takes in an element and pushes it at the first empty index it finds in the array it is used in the context of. If there are no empty spaces, the element should be pushed to the end of the array.

The arrays are data structures in JavaScript that are used to store values of the same type in a linear form. Arrays are basic data structures that every developer works on. While working with arrays, they commonly work on pushing elements in an empty index of an array. This article will guide you on how to push value in an empty index in an array in JavaScript.

Understanding the Problem

Suppose you have an array having some empty indexes in it, and you have to push values in those empty indexes.

For example: We have an array that contains some empty values inside it like this:

const arr = [43,534534,645,64,,645,64,,645,,645,,65,,645,,64];

We are required to write an Array function pushAtEmpty(). Let's write the code for this function. We will first search for the index of empty position and then replace the value there with the value we are provided with.

Implementation Steps

To achieve this, follow the steps mentioned below:

  • Define the Array: We begin by creating an array called "arr" that has some elements in it, along with a few empty indexes represented by commas.
  • Extend Array Prototype: We add a new method called pushAtEmpty to the Array.prototype. This allows all arrays to use this method.
  • Initialize an Index Variable: Inside the method, we define a variable called "index" to help us track our current position in the array as we iterate through it.
  • Loop Through the Array: We use a for loop to iterate through each index in the array, starting from 0 up to the length of the array.
  • Check for Empty Slots: Inside the loop, we check if the current index is empty (meaning it has no value). If it is empty, we add the new element in that index.
  • Insert Element: If we find an empty index, we use the splice() method to fill it with the new element. The splice() method changes the array directly.
  • Check for Full Array: After exiting the loop, we check if no empty slots were found by comparing index with this.length. If they are equal, it means we reached the end of the array without finding an empty slot.

Example

The following is an example of pushing values in an empty index in an array that is following the above steps:

const arr = [43,534534,645,64,,645,64,,645,,645,,65,,645,,64];
Array.prototype.pushAtEmpty = function(element){
let index;
for(index = 0; index < this.length; index++){
    if(arr[index] === undefined){
        this.splice(index, 1, element);
        break;
    };
};
if(index === this.length){
    this.push(element);
}
};
arr.pushAtEmpty(23);
arr.pushAtEmpty(33);
arr.pushAtEmpty(43);
arr.pushAtEmpty(53);
arr.pushAtEmpty(63);
arr.pushAtEmpty(73);
arr.pushAtEmpty(83);
arr.pushAtEmpty(93);
console.log(arr);

Output

[
43, 534534, 645, 64,
23, 645, 64, 33,
645, 43, 645, 53,
65, 63, 645, 73,
64, 83, 93
]

Conclusion

In this article, we have seen a simple method for pushing values in empty indexes in an array in JavaScript. Using this method, you can simply push values in an array having empty indexes.

Updated on: 2025-02-25T14:57:30+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements