javascript - data.length 未定义

标签 javascript jquery

我想获取数据的长度,当我尝试console.log(data)时,我得到了所有数据,但console.log(data.length)未定义。

function tampilesai(idsoal){
$.ajax({
    url: "soalesai/baca/"+idsoal,
    type: "POST",
    dataType: "JSON",
    success: function(data){
        console.log(data); //here <=================
        console.log(data.length); //here <=============

        var html = "";   
        for (i = 0; i < data.length; ++i) {     
            html += "<tr>" +
                "<td class='isitbl'>" + data[i].idsoal + "</td>" +
                "<td class='isitbl' style='width:20px;'>" + data[i].noesai + "</td>" +
                "<td class='isitbl' style='width:200px;'>" + data[i].matakuliah + "</td>" +
                "<td>" + data[i].isiesai + "</td>" +
                "<td class='isitbl'><button class='btn btn-danger btn-block' id='hapus' data[i]-id='" + data[i].idsoal + "'>" +
                "<span class='icon-trash'></span> Hapus</button>" +
                "</td>" +
                "</tr>";
        }
        $("tbody#tblesai").html(html);
    }
})

我需要 data.length 进行循环并填充我的表格。它的数据日志

{idesai: "90", idsoal: "1", noesai: "1", matakuliah: "1", isiesai: "<p>asdd</p>"}

此图像浏览器

enter image description here

最佳答案

如果您的数据是对象,那么您需要使用Object.keys(data).length来获取数据对象的长度。

关于javascript - data.length 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56516291/

相关文章:

php - 我有一个表,我用它通过 ajax 或 jquery 更新 mysql,适用于 firefox、opera、chrome 和 safari,但不适用于 IE8

javascript - 是否可以使用 JQuery .attr() 标记向类添加属性?

javascript - 访问不在数组中的 json 子对象

javascript - 如何获取和使用 window.onbeforeunload 事件的返回语句中显示的提示值

javascript - 使用 jQuery 选择 SVG 宽度?

javascript - jQuery localScroll 和 smoothScroll 不起作用

javascript - jQuery $( "").text();简单地识别文本

c# - 如何像 $.post 一样用 $.ajax 替换 div 内容

javascript - 简单的javascript问题-onMouseOver div背景更改

javascript - 比较字符串并更改行的颜色