由於返回格式限制,對象不能調用數組方法
方法:
1、Array.from(object)
object中必須有length屬性,返回的數組長度取決於length長度
key 值必須是數值
2、Object.values(object)
與第一種不同的是不需要length屬性,返回一個對象所有可枚舉屬性值
返回數組的成員順序:
const obj = { 100: 'a', 2: 'b', 7: 'c' };
Object.values(obj)
// ["b", "c", "a"]
3、Object.keys(object)
返回一個對象的自身可枚舉屬性組成的數組,數組中屬性名的排列順序和使用 for…in 循環遍歷該對象時返回的順序一致
4、Object.entries(object)
返回一個給定對象自身可枚舉屬性的鍵值對數組
const obj = { foo: 'bar', baz: 42 };
console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
5,使用 for…in…
function getObjectKeys(object) { let keys = [] for(let property in object) keys.push(property) return keys } function getObjectValues(object) { let values = [] for(let property in object) values.push(object[property]) return values }

const yearMap = resData; console.info(yearMap); this.filesTree = []; for (const year of Object.keys(yearMap)) { const yearChildren: any[] = []; const yearJSON = { id: year, label: year, children: yearChildren, }; // console.log('year', yearJSON); const monthMap = yearMap[year]; for (const month of Object.keys(monthMap)) { const monthChildren: any[] = []; const monthJSON = { id: month, label: month, children: monthChildren, }; // console.log('month', monthJSON); const fileList: any[] = monthMap[month]; fileList.forEach( (file) => { const fileJSON = { id: file.fileName, label: file.fileName, url: file.url, }; // console.log('file', fileJSON); monthJSON.children.push(fileJSON); }); // console.log('2222', monthJSON ); yearJSON.children.push(monthJSON); } // console.log('wwww', yearJSON); this.filesTree.push(yearJSON); } console.log('succtt', this.filesTree) this.defaultExpandArr.push(this.filesTree[0].children[0].id); });
