简体   繁体   English

在JavaScript中访问对象内部的数组时获取“未定义”

[英]Getting 'undefined' while accessing array inside object in javascript

I am trying to access time from given date. 我正在尝试从给定日期访问时间。

Here is HTML 这是HTML

<input type="date" name="fromdate" id="fromdate" onchange="displaytimefromdate(this.value)" />
<div id="timepicker"></div>

Here is Javascript Code: 这是Javascript代码:

    var availableTime = {"2014-07-18":["Friday, 02:02:00 - 00:00:00","Friday, 01:01:00 - 01:01:00"],"2014-07-19":["Saturday, 02:02:00 - 00:00:00","Saturday, 01:01:00 - 01:01:00"],"2014-07-20":["Sunday, 02:02:00 - 00:00:00","Sunday, 01:01:00 - 01:01:00"]};

function displaytimefromdate(timefromdate) {
        jQuery('#timepicker').html('<div class="form-group"><div class="radio"><label><input class="form-control input-lg bg-darkBlue fg-white" type="radio" id="timepicker" name="timepicker" /></label>'+availableTime.timefromdate+'</div></div>');
}

Here is JsFiddle :- http://jsfiddle.net/rW73e/ 这是JsFiddle:-http: //jsfiddle.net/rW73e/

Help would be appreciated. 帮助将不胜感激。

Thanks 谢谢

Take a look here: 在这里看看:

http://jsfiddle.net/rW73e/2/ http://jsfiddle.net/rW73e/2/

I have given a direct string value to timefromdate. 我给timefromdate一个直接的字符串值。

So, 所以,

availableTime[timefromdate];

works fine. 工作正常。 It has no problem. 没问题。

Your date object keys are limited to 2 or 3 values which you have included in the availableTime , which map directly to the time. 您的日期对象键限制为2或3个值,这些值已包含在availableTime ,它们直接映射到时间。 Other dates are not mapped. 其他日期未映射。

If you select other dates which are not present in the availableTime object, it will surely return undefined . 如果选择的其他日期不存在于availableTime对象中,则它肯定会返回undefined

If I understand correctly you are trying to get a value from your json object. 如果我正确理解,您正在尝试从json对象获取值。

Try: 尝试:

availableTime[timefromdate] 

instead of: 代替:

availableTime.timefromdate

Hope this helps 希望这可以帮助

With bracket notation it's working, however the the object you try to access defined as array. 使用括号表示法有效,但是您尝试访问的对象定义为数组。

var availableTime = {"2014-07-18":["Friday, 02:02:00 - 00:00:00","Friday, 01:01:00 - 01:01:00"],"2014-07-19":["Saturday, 02:02:00 - 00:00:00","Saturday, 01:01:00 - 01:01:00"],"2014-07-20":["Sunday, 02:02:00 - 00:00:00","Sunday, 01:01:00 - 01:01:00"]};

function displaytimefromdate(timefromdate) {
    jQuery('#timepicker').html('<div class="form-group"><div class="radio"><label><input class="form-control input-lg bg-darkBlue fg-white" type="radio" id="timepicker" name="timepicker" /></label>'+availableTime[timefromdate]+'</div></div>');
}

http://jsfiddle.net/rW73e/3/ http://jsfiddle.net/rW73e/3/

If you want to use the entire block, you should rearrange the object as: 如果要使用整个块,则应将对象重新排列为:

var availableTime = {"2014-07-18":["Friday, 02:02:00 - 00:00:00, Friday, 01:01:00 - 01:01:00"],"2014-07-19":["Saturday, 02:02:00 - 00:00:00, Saturday, 01:01:00 - 01:01:00"],"2014-07-20":["Sunday, 02:02:00 - 00:00:00, Sunday, 01:01:00 - 01:01:00"]};

http://jsfiddle.net/rW73e/4/ http://jsfiddle.net/rW73e/4/

... or access the value within the array: ...或访问数组中的值:

function displaytimefromdate(timefromdate) {
        jQuery('#timepicker').html('<div class="form-group"><div class="radio"><label><input class="form-control input-lg bg-darkBlue fg-white" type="radio" id="timepicker" name="timepicker" /></label>'+availableTime[timefromdate][0]+'</div></div>');

http://jsfiddle.net/rW73e/5/ http://jsfiddle.net/rW73e/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM