简体   繁体   English

JS中嵌套列表项中的多维数组或json对象

[英]Multi-dimensional array or json object from nested list item in JS

I've a nested list items. 我有一个嵌套的清单项目。 I want to make an multi-dimensional array or json object from the list items. 我想从列表项中创建一个多维数组或json对象。 I've tried something like below. 我已经尝试过类似下面的内容。 But I'm not getting the expected output. 但是我没有得到预期的输出。

The depth of the list item can be more. 列表项的深度可以更大。 So, I'll define a recursive method to do this later. 因此,稍后我将定义一个递归方法来执行此操作。

DEMO Fiddle 演示小提琴

HTML: HTML:

<div class="dd" id="nestable">
    <ol class="dd-list">
        <li class="dd-item" data-id="1">
            <div class="dd-handle">Sub-indicator 1</div>
            <ol class="dd-list">
                <li class="dd-item" data-id="2">                    
                    <div class="dd-handle">Sub-indicator 2</div>
                    <ol class="dd-list">
                        <li class="dd-item" data-id="3">

                            <div class="dd-handle">Sub-indicator 3</div>
                            <ol class="dd-list">
                                <li class="dd-item" data-id="4">

                                    <div class="dd-handle">Sub-indicator 4</div>
                                </li>
                            </ol>
                        </li>
                    </ol>
                </li>
                <li class="dd-item" data-id="3">

                            <div class="dd-handle">Sub-indicator 3</div>
                            <ol class="dd-list">
                                <li class="dd-item" data-id="4">

                                    <div class="dd-handle">Sub-indicator 4</div>
                                </li>
                            </ol>
                        </li>
            </ol>
        </li>
    </ol>
</div>

JS: JS:

var subIndicTreeObj = {};
var tempObj = [];
var parentId = 0;
var parentId1 = 0;
var parentId2 = 0;

$('#nestable > ol > li').each(function(index, value) { 
    parentId = 0;
    parentId1 = 0;
    parentId2 = 0;

    tempObj.push({'sub_indic_id': $(this).attr('data-id'), 'parent_id': parentId});


    if ($(this).has('ol').length > 0) { 

        parentId = $(this).attr('data-id');

        $(this).find('ol > li').each(function(index1, value1) { 

            tempObj.push({'sub_indic_id': $(this).attr('data-id'), 'parent_id': parentId});


            if ($(this).has('ol').length > 0) {  

                parentId1 = $(this).attr('data-id');

                $(this).find('ol > li').each(function(index2, value2) { 

                    tempObj.push({'sub_indic_id': $(this).attr('data-id'), 'parent_id': parentId1});

                    if ($(this).has('ol').length > 0) { 


                        parentId2 = $(this).attr('data-id');

                        $(this).find('ol > li').each(function(index3, value3) { 

                            tempObj.push({'sub_indic_id': $(this).attr('data-id'), 'parent_id': parentId2});

                        });
                    }

                });
            } 
        });
    } 
});

subIndicTreeObj = tempObj;

console.log(subIndicTreeObj);

Current Output: 电流输出:

 [
  Object{
    sub_indic_id="1",
    parent_id=0
  },
  Object{
    sub_indic_id="2",
    parent_id="1"
  },
  Object{
    sub_indic_id="3",
    parent_id="2"
  },
  Object{
    sub_indic_id="4",
    parent_id="3"
  },
  Object{
    sub_indic_id="4",
    parent_id="2"
  },
  Object{
    sub_indic_id="3",
    parent_id="1"
  },
  Object{
    sub_indic_id="4",
    parent_id="3"
  },
  Object{
    sub_indic_id="4",
    parent_id="1"
  },
  Object{
    sub_indic_id="3",
    parent_id="1"
  },
  Object{
    sub_indic_id="4",
    parent_id="3"
  },
  Object{
    sub_indic_id="4",
    parent_id="1"
  }
]

I want the immediate parent_id of a child. 我想要一个孩子的直接parent_id。 But I'm getting the top level parent_id for some list item. 但是我正在获取某些列表项的顶级parent_id。 Such as, 如,

Object{
    sub_indic_id="4",
    parent_id="1"
  }

Can anybody help to find out the problem ? 有人可以帮忙找出问题所在吗? Thanks in advance. 提前致谢。

I didn't understand if it's the expected behaviour, anyway, it's recursive, and also find the first child. 我不明白这是否是预期的行为,无论如何,它是递归的,并且也找到了第一个孩子。 I just added a > before the ol > li selector. 我只是在ol > li选择器之前添加了>

Note that i also added a data-id='0' to id="nestable" to get the first parent id. 请注意,我还向id="nestable"添加了data-id='0'以获取第一个父代ID。

edit 编辑

For more detail about > selector as asked in comment, i suggest to have look to some documentation. 有关注释中所要求的>选择器的更多详细信息,我建议查看一些文档。 I mostly learned from w3school . 我主要从w3school学习。 Reporting from that link 通过该链接进行报告

The element>element selector is used to select elements with a specific parent. element> element选择器用于选择具有特定父元素的元素。

So, in your code with ol > li you were retriving all the li children of every ol , contained in $(this) . 所以,在你的代码ol > li你retriving的所有 li每个儿童ol ,包含在$(this)

With > ol > li you retrive the direct children of the object on which you're searching, and not also the children of chidren. 使用> ol > li ,您将检索要搜索的对象的直接子级,而不是儿童的子级。

 var subIndicTreeObj = []; function findLiChild($obj, parentId) { $obj.find('> ol > li').each(function(index1, value1) { subIndicTreeObj.push({ 'sub_indic_id': $(this).attr('data-id'), 'parent_id': parentId }); findOlChild($(this)); }); } function findOlChild($obj) { if ($obj.has('ol').length > 0) { findLiChild($obj, $obj.attr('data-id')); } } findOlChild($('#nestable')); console.log(subIndicTreeObj); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="dd" id="nestable" data-id="0"> <ol class="dd-list"> <li class="dd-item" data-id="1"> <div class="dd-handle">Sub-indicator 1</div> <ol class="dd-list"> <li class="dd-item" data-id="2"> <div class="dd-handle">Sub-indicator 2</div> <ol class="dd-list"> <li class="dd-item" data-id="3"> <div class="dd-handle">Sub-indicator 3</div> <ol class="dd-list"> <li class="dd-item" data-id="4"> <div class="dd-handle">Sub-indicator 4</div> </li> </ol> </li> </ol> </li> <li class="dd-item" data-id="3"> <div class="dd-handle">Sub-indicator 3</div> <ol class="dd-list"> <li class="dd-item" data-id="4"> <div class="dd-handle">Sub-indicator 4</div> </li> </ol> </li> </ol> </li> </ol> </div> 

Try a simple loop: 尝试一个简单的循环:

var data = [];//create the array
$('.dd-item').each(function() {//loop each item
  var parent = $(this).parent().closest('.dd-item');//get the parent
  var parent_id = 0;//set 0 if it has no parent
  if (parent.length) {
    parent_id = parent.attr('data-id');//set parent id if it has
  }
  data.push({
    parent_id: parent_id ,sub_indic_id: $(this).attr('data-id')
  });//append to the array
});
console.log(data);

demo: https://jsfiddle.net/uwqqmz86/ 演示: https : //jsfiddle.net/uwqqmz86/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM