Vue Js Remove Leading Zero from Array:In Vue.js, you can remove starting zeros from an array by using a while loop and finding the index of the first non-zero element. After finding the index, you can create a new array that starts from that index and includes all the remaining elements. This will result in a new array without any starting zeros.
This Vue.js code creates an app with two data properties: originalArray
and nonZeroArray
. The originalArray
is an array of numbers with leading zeros. The nonZeroArray
is initially an empty array.
The mounted
hook is used to modify the nonZeroArray
property by removing the leading zeros from the originalArray
. This is achieved by creating a copy of the originalArray
using the spread operator ([...this.originalArray]
), and then using a while
loop to check if the first element of the array is 0
. If it is, then the first element is removed from the array using the shift()
method. The loop continues until the first element is no longer 0
. Finally, the nonZeroArray
data property is set to the modified array.
Vue Js Remove Leading Zero from Array Example
<div id="app">
<p>Original Array: {{ originalArray }}</p>
<p>Array with leading zeros removed: {{ nonZeroArray }}</p>
</div>
<script type="module">
const app = Vue.createApp({
data() {
return {
originalArray: [0, 0, 0, 4, 3, 4, 0, 0, 9, 1],
nonZeroArray: []
}
},
mounted() {
let array = [...this.originalArray]; // create a copy of the original array to work with
while (array[0] === 0) {
array.shift();
}
this.nonZeroArray = array; // set the nonZeroArray data property to the modified array
}
})
app.mount('#app')
</script>