javascript - 输出对象的属性

标签 javascript jquery arrays object

我正在使用以下 JavaScript:

    var hours = $(".input_hr");
    var minutes = $(".input_min");
    var categories = $(".input_cat");
    for(var i=0;i<categories.length;i++){
        if (categories[i].value === "Entertainment") {
            category_hours.Entertainment.push({hour:hours[i].value,minute:minutes[i].value});//add object literal
        };

    }

像这样构建我的数据:

category_hours = {

  Entertainment = [{hour:   ,minute:  }]
}

我想控制台记录“小时”属性。我尝试过使用:

jQuery.each(category_hours, function() {
              console.log(Entertainment.hour)
        }) 

但我不断收到一条错误消息,显示“娱乐未定义”。

如果我这样做 console.log(this.Entertainment.hour) 我得到“小时属性未定义”

最佳答案

您必须循环访问 Entertainment 数组,因为您的 category_hours 不是一个数组,它被定义为一个对象:

category_hours = {

所以这可能有效:

jQuery.each(category_hours.Entertainment, function () {
    console.log(this.hour);
});

关于javascript - 输出对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925185/

相关文章:

javascript - 如何在取消选中时删除对象的属性?

javascript - 如何以特定格式对数字进行四舍五入?

javascript - jQuery:如何选择所有带有名称的单选按钮?

javascript - 将给定结构二维数组转换为嵌套对象数组

arrays - 数组上的 PartialEq

python - 在 Python 中将整数列表转换为字节数组

javascript - 从 promise 中返回另一个 promise

Javascript Uncaught TypeError : $(. ..).dialog 不是函数

javascript - 在 XSL 中处理 HTML 标签

javascript - Mozilla 中带有 formatBlock 的 WYSIWYG 开发错误