vuejs中的可折叠类别树

时间:2018-02-04 05:37:23

标签: javascript arrays vue.js

我正在构建一个小项目,我试图以可折叠树的形式制作类别过滤器。问题是我的数据库中的数据是这样的。

["Any Department->Electronics->Accessories->","Any Department->Electronics->Cameras & Photography->","Any Department->Electronics->Car & Vehicle Electronics->","Any Department->Electronics->Computers & Accessories->","Any Department->Electronics->GPS & Accessories->","Any Department->Electronics->Hi-Fi & Home Audio->","Any Department->Electronics->Hi-Fi & Home Audio->Home Theater->","Any Department->Electronics->Mobiles & Tablets->","Any Department->Electronics->Portable Media Players->","Any Department->Electronics->Telephones & Accessories->","Any Department->Electronics->Warranties->","Any Department->Electronics->Wearable Technology->","Any Department->Electronics->Accessories->Blank Media->","Any Department->Electronics->Accessories->Camera & Photo Accessories->","Any Department->Electronics->Accessories->Car & Vehicle Electronics Accessories->","Any Department->Electronics->Accessories->Computer Accessories->","Any Department->Electronics->Accessories->General Purpose Batteries & Battery Chargers->","Any Department->Electronics->Accessories->Home Audio & Video Accessories->","Any Department->Electronics->Accessories->Memory Cards->","Any Department->Electronics->Accessories->Mobile Accessories->","Any Department->Electronics->Accessories->Navigation Accessories->"].

我希望数据看起来像这样:

let tree = {
  label: 'root',
  nodes: [
    {
      label: 'item1',
      nodes: [
        {
          label: 'item1.1'
        },
        {
          label: 'item1.2',
          nodes: [
            {
              label: 'item1.2.1'
            }
          ]
        }
      ]
    }, 
    {
      label: 'item2'  
    }
  ]
}

0 个答案:

没有答案