Javascript 从js上的json对象递归创建树菜单

Javascript 从js上的json对象递归创建树菜单,javascript,jquery,json,Javascript,Jquery,Json,我怎样才能得到菜单树 技术->电视->索尼->某些型号的acd 技术->电视->索尼->某些型号的bcd 技术->电视->LG->某些型号的bcd 技术->电视->LG->某些型号的zcd 手机->智能手机->苹果->某些型号 手机->智能手机\u小型显示->苹果->一些型号1 手机->智能手机\u小型显示->三星->部分型号2 手机->智能手机\大型显示->苹果->部分型号3 手机->智能手机\大型显示->三星->部分型号4 我需要一个带有层次结构或html菜单代码的数组或对象。 php上的输

我怎样才能得到菜单树

技术->电视->索尼->某些型号的acd

技术->电视->索尼->某些型号的bcd

技术->电视->LG->某些型号的bcd

技术->电视->LG->某些型号的zcd

手机->智能手机->苹果->某些型号

手机->智能手机\u小型显示->苹果->一些型号1

手机->智能手机\u小型显示->三星->部分型号2

手机->智能手机\大型显示->苹果->部分型号3

手机->智能手机\大型显示->三星->部分型号4

我需要一个带有层次结构或html菜单代码的数组或对象。 php上的输出:

[
    {
        "type": "technic",
        "product_cat": "TV",
        "brand_name":"Sony",
        "Model":"Some_model_acd",
    },
    {
        "type": "technic",
        "product_cat": "TV",
        "brand_name":"Sony",
        "Model":"Some_model_bcd",
    },
    {
        "type": "technic",
        "product_cat": "TV",
        "brand_name":"LG",
        "Model":"Some_model_zcd",
    }, 
    {
        "type": "technic",
        "product_cat": "TV",
        "brand_name":"LG",
        "Model":"Some_model_bcd",
    },  
    {
        "type": "phones",
        "product_cat": "smartphones",
        "brand_name":"Apple",
        "Model":"some_model",
    },  
    {
        "type": "phones",
        "product_cat": "smartphones_small_disp",
        "brand_name":"Apple",
        "Model":"some_model 1",
    },  
    {
        "type": "phones",
        "product_cat": "smartphones_small_disp",
        "brand_name":"Samsung",
        "Model":"some_model 2",
    }, 
    {
        "type": "phones",
        "product_cat": "smartphones_large_disp",
        "brand_name":"Apple",
        "Model":"some_model 3",
    },  
    {
        "type": "phones",
        "product_cat": "smartphones_large_disp",
        "brand_name":"Samsung",
        "Model":"some_model 4",
    },
]

您可以不用递归,而是使用。通过使用
.reduce()
可以根据属性创建嵌套对象,并将所需的
模型添加到数组中

见下例:

const arr=[{type:“technic”,product_cat:“TV”,brand_name:“Sony”,Model:“Some_Model_acd”},{type:“technic”,product_cat:“TV”,brand_name:“Sony”,Model:“Some_Model_bcd”},{type:“technic”,product_cat:“TV”,brand:“LG”,Model:“Some_Model_bcd”},product:“智能手机”,品牌名称:“苹果”,型号:“一些型号”},{type:“手机”,产品类别:“智能手机小排量”,品牌名称:“苹果”,型号:“一些型号1”},{type:“手机”,产品类别:“智能手机小排量”,品牌名称:“三星”,型号:“一些型号2”},{type:“手机”,产品类别:“智能手机大排量”,品牌名称:“苹果”,型号:“一些型号3”},{type:“手机”,产品类别:“智能手机”,品牌名称:“三星”,型号:“一些型号4”};
const menu_tree=arr.reduce((a,{type,product_cat,brand_name,Model})=>{
a[type]=(a[type]|{});
a[type][product_cat]=(a[type][product_cat]|{});
常数x=a[类型][产品类别][品牌名称];
a[类型][产品类别][品牌名称]=x?[…x,型号]:[型号];
返回a;
}, {});
控制台日志(菜单树)您可以参考此
var数据=[
{
“类型”:“技术”,
“产品类别”:“电视”,
“品牌名称”:“索尼”,
“模型”:“一些模型”,
},
{
“类型”:“技术”,
“产品类别”:“电视”,
“品牌名称”:“索尼”,
“模型”:“一些模型”,
},
{
“类型”:“技术”,
“产品类别”:“电视”,
“品牌名称”:“LG”,
“模型”:“一些模型”,
}, 
{
“类型”:“技术”,
“产品类别”:“电视”,
“品牌名称”:“LG”,
“模型”:“一些模型”,
},  
{
“类型”:“电话”,
“产品类别”:“智能手机”,
“品牌名称”:“苹果”,
“模型”:“一些模型”,
},  
{
“类型”:“电话”,
“产品类别”:“智能手机小型显示”,
“品牌名称”:“苹果”,
“模型”:“一些模型1”,
},  
{
“类型”:“电话”,
“产品类别”:“智能手机小型显示”,
“品牌名称”:“三星”,
“模型”:“一些模型2”,
}, 
{
“类型”:“电话”,
“产品类别”:“智能手机大屏幕显示”,
“品牌名称”:“苹果”,
“模型”:“一些模型3”,
},  
{
“类型”:“电话”,
“产品类别”:“智能手机大屏幕显示”,
“品牌名称”:“三星”,
“模型”:“一些模型4”,
},
]
对于(变量i=0;i”+数据[i]。产品类别+“->”+数据[i]。品牌名称+“->”+数据[i]。型号)

}
事实并非如此。我更新了这个问题,并给出了一个php输出示例。
Array (
    ['technic'] =>  Array (
        ['TV'] =>   Array (
            ['Sony']  => Array (
                [0]  => Some_model_acd,
                [1]  => Some_model_bcd
            ),
            ['LG']  => Array (
                [0]  => Some_model_zcd,
                [1]  => Some_model_bcd
            )
        )
    ),
    ['phones'] =>   Array (
        ['smartphones'] =>  Array (
            ['Apple']  => Array (
                [0]  => some_model
            )
        ),
        ['smartphones_small_disp'] =>   Array (
            ['Apple']  => Array (
                [0]  => some_model 1,
            ),
            ['Samsung']  => Array (
                [0]  => some_model 2,
            )
        ),
        ['smartphones_large_disp'] =>   Array (
            ['Apple']  => Array (
                [0]  => some_model 3,
            ),
            ['Samsung']  => Array (
                [0]  => some_model 4,
            )
        )
    )
)