1

I have a select input that it has a function, I'd like to do when it changes it empty the array:

My HTML code:

<select class="form-control" id="exampleFormControlSelect1"
      v-model="form.type_id"
      v-on:change="getType"
 >
     <option :value="null">-Seleccionar-</option>
     <option :value="1">Entrada</option>
     <option :value="2">Salida</option>
 </select>

How you can see it has a function getType, and my vuejs code:

export default {
    data: function() {
        return {
            entrance_inputs: [{
                product: null,
                quantity: 0,
                cost: 0
            }],
            k: 0,
        }
    },
    methods: {
        addEntranceProduct () {
            this.entrance_inputs.push({
                product: null,
                quantity: 0,
                cost: 0
            })
            console.log(this.inputs)
        },
        removeEntranceProduct (index) {
            this.entrance_inputs.splice(index, 1)
        },
        getType() {
            this.type_id_value = this.form.type_id;
            entrance_inputs.splice(0);
        }
    },
    computed: {
        isDisabled() {
            return true;
        }
    }
}

I have an array entrance_inputs that I add elements how you can see in addEntranceProduct, BUT in getType function I'd like to add something that it clears the entire array and it removes all elements from it, how could I do it? Thanks

2
  • Then simply override it with an empty array: this.entrance_inputs = [] Commented Jun 28, 2021 at 13:48
  • this.entrance_inputs.length = 0 Commented Jun 28, 2021 at 13:49

1 Answer 1

1

as already suggested by @Cheetah and @Amir, there are multiple ways:

1. override it

this.entrance_inputs = [];

2. Set length to 0

this.entrance_inputs.length = 0;

3. splice the whole array

this.entrance_inputs.splice(0, this.entrance_inputs.length);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.