Array Length returns 0

后端 未结 4 939
旧巷少年郎
旧巷少年郎 2020-12-11 16:24

Could anyone help explain why length is returning 0? Thank you VERY much in advance!

var errors = [];
errors[\'\'] = \"Monday Morning slot already taken. Wou         


        
4条回答
  •  时光说笑
    2020-12-11 16:47

    Your Array needs a numeric input, you're currently using an empty string as an Index, which is invalid.

    You can either specify an index, initialise the Array with your given elements, or push that item to the Array:

    // Option 1: Specify index
    var errors = [];
    errors[0] = 'Monday Morning slot already taken. Would you like to replace it?';
    
    // Options 2: Initialise the Array with values
    var errors = ['Monday Morning slot already taken. Would you like to replace it?'];
    
    // Option 3: Push the item to Array
    var errors = [];
    var myString = 'Monday Morning slot already taken. Would you like to replace it?';
    errors.push(myString);
    

提交回复
热议问题