我如何得到数组中对象的位置

how do i get the position of the objects in my array?

本文关键字:对象 位置 数组 何得      更新时间:2023-09-26

我不想在我的数组中获得对象的顺序,所以我可以将它们存储到我的数据库?但是我不知道该在for循环中写些什么。

下面是代码

<script>
    $(document).ready(function () {
        var rootLimit = 8;
        $('ul.sortable').nestedSortable({
            handle: 'a',
            items: 'li',
            listType: 'ul',
            maxLevels: '3',
            toleranceElement: '> a',
            update: function (event, ui) {
                list = $(this).nestedSortable(
                    'toHierarchy', 
                    { startDepthCount: 0 }
                );
                var page_id = ui.item.find('> a').attr('data-page-id');
                console.log(list);
                for (var i = 0; i < list.length; i++) {
                    //Do something
                }
                $.post('/page/updatemenu/' + page_id, { 
                    list : list 
                }, function (data) {
                });
            }
        });
    });
</script>

可以使用jQuery方法.each():

代替for
list.each(function(index, item){
    // do something with item or index of item
});

Use -

var index = $(ui.sender).index();