[Vuejs]-Vue js recursively flatten nested parent-children object

0👍

Use arrow functions instead of function keyword, to keep this from your Vue. instance scope. Function creates it’s own scope thus you lose access to outer 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'
})

Leave a comment