maplol123@gmail.com,
<script>
let data = [{
id: 1,
name: ["bag", "first hand", "first adding"],
style: 0,
adding: 0,
},
{
id: 2,
name: ["bag", "first hand", "second adding"],
style: 0,
adding: 1,
},
{
id: 3,
name: ["sac", "first hand", "first adding"],
style: 0,
adding: 0,
}
];
let findName = (key, arr) => arr.find(({
name
}) => name === key);
let createArr = data => data.reduce((arr, ob) => {
let item = arr,
name,
last = ob.name.pop();
for (name of ob.name) {
let obj = findName(name, item)
if (!obj) {
obj = {
name,
sec_name: []
}
item.push(obj)
}
item = obj.sec_name
}
ob.name = last;
item.push(ob)
return arr
}, [])
let arr = createArr(data)
document.write(`<pre>${JSON.stringify(arr, "", 1)}</pre>`)
</script>