简体   繁体   English

Vue JS递归展平嵌套的父子对象

[英]vue js recursively flatten nested parent-children object

I am trying to flatten the following data , but apparently my recursive function in vue method is not working properly. 我正在尝试展平以下数据,但是显然我的vue方法中的递归函数无法正常工作。 While I was debugging I noticed that once It enters the reduce function the 'this' variable change into something else(originally vue component). 在调试时,我注意到,一旦它进入reduce函数,“ this”变量就会变成其他东西(最初是vue组件)。

{
  "id":1,
  "level":"1",
  "text":"Sammy",
  "type":"Item",
  "children":[
     {
        "id":11,
        "level":"2",
        "text":"Table",
        "type":"Item",
        "children":[
           {
              "id":111,
              "level":"3",
              "text":"Dog",
              "type":"Item",
              "children":null
           },
           {
              "id":112,
              "level":"3",
              "text":"Cat",
              "type":"Item",
              "children":null
           }
        ]
     },
     {
        "id":12,
        "level":"2",
        "text":"Chair",
        "type":"Item",
        "children":[
           {
              "id":121,
              "level":"3",
              "text":"Dog",
              "type":"Item",
              "children":null
           },
           {
              "id":122,
              "level":"3",
              "text":"Cat",
              "type":"Item",
              "children":null
           }
        ]
     }
  ]
 }

desired result 理想的结果

 {
   "id":1,
   "level":"1",
   "text":"Sammy",
   "type":"Item",
   "children":[]
 }
 {
   "id":11,
   "level":"2",
   "text":"Table",
   "type":"Item",
   "children":[]
 }
 ...

https://jsfiddle.net/hr8dhy8n/11/ here is my repo. https://jsfiddle.net/hr8dhy8n/11/这是我的仓库

 // https://stackoverflow.com/q/47961578/3397771 var data =[ { "id":1, "level":"1", "text":"Sammy", "type":"Item", "children":[ { "id":11, "level":"2", "text":"Table", "type":"Item", "children":[ { "id":111, "level":"3", "text":"Dog", "type":"Item", "children":null }, { "id":112, "level":"3", "text":"Cat", "type":"Item", "children":null } ] }, { "id":12, "level":"2", "text":"Chair", "type":"Item", "children":[ { "id":121, "level":"3", "text":"Dog", "type":"Item", "children":null }, { "id":122, "level":"3", "text":"Cat", "type":"Item", "children":null } ] } ] } ] // define the item component Vue.component('item', { methods: { flattenInformation: function(a, b) { return a.reduce(function (p, c) { !!c.children ? (p.push(c), this.flattenInformation(c.children, p), c.children = []) : p.push(c);return p; }, b); }, getLengthNow (model) { var list = []; list.push(model); var flatten = this.flattenInformation(list,[]); } }, props: ['model'], template: '#item-template' }) // boot up the demo var demo = new Vue({ data: { nestedData: data }, el: '#demo' }); 
 <!-- item template --> <script type="text/x-template" id="item-template"> <template> {{this.getLengthNow(this.model)}} </template> </script> <!-- the demo root element --> <ul id="demo"> <item class="item" :model="nestedData[0]"> </item> </ul> 

Use arrow functions instead of function keyword, to keep this from your Vue. 使用箭头的功能,而不是function的关键字,让this从您的Vue公司。 instance scope. 实例范围。 Function creates it's own scope thus you lose access to outer this :-) Function创建它自己的范围内,您就输到外部入this :-)

Vue.component('item', {
  methods: {
flattenInformation: (a, b) => {
            return a.reduce((p, c) => {
            !!c.children ? (p.push(c), this.flattenInformation(c.children, p), c.children = []) : p.push(c);return p;
         }, b));
        },
        getLengthNow (model) {
        var list = [];
        list.push(model);
        var flatten = this.flattenInformation(list,[]);

    }
  },
  props: ['model'],
  template: '#item-template'
})

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

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