javascript - 访问js集合中的第一个元素

标签 javascript jquery

在我的 js 函数中,我有以下内容

success: function (result) {
     $.each(result, function (i, item) {                  
        $("#tab-" + item.myType).append(result);
     })
}

现在我已经更改了这个函数,而是有以下内容

success: function (result) {
   $("#tab-how To Access to the first result collection element ").html(result);
 }
}

我尝试过

$("#tab-" + result[0].item.myType).html(result);

但这并没有帮助。

问题是:如何访问结果集合对象中的第一个js元素

有什么想法吗?

使用 console.log(result) 我得到了在部分 View 上显示的 html 片段

div class="product clearfix">
        <div class="l-new">
            <a href="#">
                @Model.MyType (rendered as My Type)
            </a> 
....
</div>

最佳答案

你可以尝试:

result[0] // if you have numeric indices in your result collection

或者

var firstItem = null;
for(var x in result) { // if you have named properties
    firstItem = result[x];
    break;
}

关于javascript - 访问js集合中的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14056779/

相关文章:

javascript - 当单选按钮被选中时,如何通过其标签名称获取单选按钮名称和 ID?

javascript - 在 jQuery 嵌套插件中使用百分比

javascript - 在 Laravel 中刷新页面时删除 jQuery 中的 cookie

javascript - ES 6 类 : Access "this" in another scope

javascript - 如何从 Trello API 检索 token

javascript - 能否使用 Protractor 测试 Knockout 应用程序?

javascript - 有什么方法可以查看代码的哪一部分使网页速度变慢?

javascript - 如何在 ui-router $stateChangeStart 中等待动画结束?

php - 使用 jqGrid 制作考勤表

javascript - 单击 Codeigniter 中的“编辑”按钮时更新 Bootstrap 表