0

I want to find whether an array is empty or not. If it is empty I want to add data and if it is not it will not allow adding. I am currently stuck at the following stage,

<script type="text/javascript">
export default {
    name: 'CarrierCosts',
    data () {
        return {
            inputs: [],

            form: {
                free_shipping: '',
                add_handling_costs: '',
                tax: '',
                range: '',
                checkboxValue: '',
            }
        }
    },
    methods: {
        addRange(){         
            this.inputs.push({
                rangField: '',
            })
        },
    }
}

2
  • check this.inputs.length and do whatever you want Commented Nov 28, 2018 at 13:07
  • I need to know that before to push to an array. But its not working Commented Nov 28, 2018 at 13:20

1 Answer 1

3

You could know that array is empty by checking its length:

methods: {
    addRange(){  
     if(this.inputs.length===0){       
        this.inputs.push({
            rangField: '',
        })
        }
    },
}
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.