Home  >  Article  >  Web Front-end  >  js performs tree partitioning of json data

js performs tree partitioning of json data

小云云
小云云Original
2018-03-07 13:48:491333browse

This article mainly shares with you the example code of js dividing the returned json data into a tree. I hope it can help you.

 $.ajax({
    type: "get",
    url: 'data.json',
    success:function(data){
    var jsondata = JSON.parse(data); //返回的数据是json字符串,把它转换成json对象,如果是json对象则不需转换
    var topArr = []; //声明一个数组,存储所有的json数据
    for (var i = 0; i< jsondata.length; i++) {        if (jsondata[i].parentid == 0) {
            jsondata[i].data = []; //给一级菜单下面添加data数据,存放二级菜单
            topArr.push(jsondata[i]);  //得到一级菜单
        }
    }    for (var j = 0; j < topArr.length; j++) {       for (var i = 0; i < jsondata.length; i++) {           if (jsondata[i].parentid == topArr[j].id) {
               jsondata[i].data = [];
               topArr[j].data.push(jsondata[i]);  //把获取的二级菜单存放到一级菜单的data对象中
           }
       }
   }    for (var j = 0; j < topArr.length; j++) {        for (var m = 0; m < topArr[j].data.length; m++) {            for (var i = 0; i < jsondata.length; i++) {                if (jsondata[i].parentid == topArr[j].data[m].id)    {
                   topArr[j].data[m].data.push(jsondata[i]); //把获取的三级菜单存放到二级菜单的data对象中
                }
            }
        }
    }
    }
});
               
 $.ajax({
    type: "get",
    url: &#39;data.json&#39;,
    success:function(data){
    var jsondata = JSON.parse(data); //返回的数据是json字符串,把它转换成json对象,如果是json对象则不需转换
    var topArr = []; //声明一个数组,存储所有的json数据
    for (var i = 0; i< jsondata.length; i++) {        if (jsondata[i].parentid == 0) {
            jsondata[i].data = []; //给一级菜单下面添加data数据,存放二级菜单
            topArr.push(jsondata[i]);  //得到一级菜单
        }
    }    for (var j = 0; j < topArr.length; j++) {       for (var i = 0; i < jsondata.length; i++) {           if (jsondata[i].parentid == topArr[j].id) {
               jsondata[i].data = [];
               topArr[j].data.push(jsondata[i]);  //把获取的二级菜单存放到一级菜单的data对象中
           }
       }
   }    for (var j = 0; j < topArr.length; j++) {        for (var m = 0; m < topArr[j].data.length; m++) {            for (var i = 0; i < jsondata.length; i++) {                if (jsondata[i].parentid == topArr[j].data[m].id)    {
                   topArr[j].data[m].data.push(jsondata[i]); //把获取的三级菜单存放到二级菜单的data对象中
                }
            }
        }
    }
    }
});

Related recommendations:

How to read json data code in php

Detailed explanation of how PHP compresses the returned JSON data and outputs it with gzip

Detailed example of how JavaScript sorts and searches JSON data

The above is the detailed content of js performs tree partitioning of json data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn