useEffect(() => { if (value) { const arr = value; for (const k in arr) { console.log(k, arr[k]); arr[k].key = arr[k].id; arr[k].title = arr[k].name; for (const k2 in arr[k].children) { arr[k2].key = arr[k2].id; arr[k2].title = arr[k2].name; } } // console.log("arr ==>", arr); seData2(arr); } }, [value]);
今天晚上发现ts中无法用forEach遍历对象嵌套数组的数据,比如typeof arr 为 object,就只能用for in 或者Object.entries 再或者Object.keys。
value的数据结构:
const value = [ { title: 'title1', key: 1, childern: [ { title: 'title11', key: 11, childern: [] } ] }, { title: 'title2', key: 2, } ]
标签:24,10,arr,const,title,ts,value,k2,key From: https://www.cnblogs.com/iuniko/p/17785662.html