<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<div>
{{newArray}}
</div>
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
data () {
return {
array : [
{ id : 1 },
{ parentId : 1 ,id :2 },
{ id : 3 },
{ parentId : 3 , id : 4 },
{ parentId : 4 ,id : 5 },
{ parentId : 5 ,id : 6 },
{ parentId : 6 ,id : 7 },
],
newArray : [
// { id : 1 , children :[]},
// { id : 3 , children :[]}
],
}
},
created () {
this.makedata()
},
methods : {
makedata (){
this.array.forEach( item1=> {
// console.log(Object.keys(item1).length);
if (Object.keys(item1).length=='1') {
this.newArray.push({ id : item1.id , children :[]})
}
})
this.makethree( this.newArray)
},
makethree( data ){
data.forEach( item => {
this.array.forEach ( item1 => {
if (item.id == item1.parentId) {
item.children.push({...item1 , children : []})
this.makethree( item.children)
}
})
})
},
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>