JavaScript将键/值列表匹配到几天

我正在从名为Jackrabbit的类管理系统的JSON端点创建一个类列表。我从中提取的端点是https://app.jackrabbitclass.com/jr3.0/Openings/OpeningsJSON?orgID=537284(这包括我的示例类清单)。从上一个问题中,我得到了将其付诸实践的帮助。但是,现在我也试图显示上课的日期,但是JSON端点会格式化上课的日期,如下所示:"meeting_days":{"mon":false,"tue":false,"wed":false,"thu":false,"fri":true,"sat":false,"sun":false}。我正在尝试对此进行转换,如果它说"mon":true它将在表中显示为星期一(或星期一)。如果确实是几天,我想列出它们。

这是我目前拥有的javascript / html:https : //jsfiddle.net/pikles/uqca9df0/28/

function tConvert(time) {

    // Check correct time format and split into components

    time = time.toString().match(/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];


    if (time.length > 1) { // If time format correct

        time = time.slice(1); // Remove full string match value

        time[5] = +time[0] < 12 ? 'AM' : 'PM'; // Set AM/PM

        time[0] = +time[0] % 12 || 12; // Adjust hours

    }

    return time.join(''); // return adjusted time or original string

}


function dictionaryDay(dictDay) {

    var days = ""

    for (var day in dictDay) {

        if (dictDay.hasOwnProperty(day) === true) {

            days = days + day

        } else {}

    }

    return days

}

我目前正在努力解决的有问题的代码就是dictionaryDay功能。

但是,这会在返回日期时列出每一天。我想这可能是其中一个问题=== true应该是=== "true",但是当我这样做-没有返回天。

有人能指出我正确的方向吗?


料青山看我应如是
浏览 126回答 1
1回答

繁花不似锦

Object.hasOwnProperty如果对象包含属性,则使用该返回true。它不读取值,而仅读取用于检查对象中是否存在该值的键。这是您的函数已更正:function dictionaryDay (dictDay) {&nbsp; var days = ''&nbsp; for (var day in dictDay) {&nbsp; &nbsp; // day is the key name ('mon'|'thu'|'wen'...)&nbsp; &nbsp; // dictDay['mon'] make reference to the value (true|false)&nbsp; &nbsp; if (dictDay[day]) {&nbsp; &nbsp; &nbsp; days = days + ' ' + day&nbsp; &nbsp; }&nbsp; }&nbsp;return days}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript