数组扁平化与深拷贝工具方法
原始数据
const menu = [
{
group: "基本信息" ,
title: "基本信息" ,
path: "/Basemessage" ,
meta: {
icon: "../assets/img/navicon/icon02.png" ,
iconColor: "../assets/img/navicon/icon12.png" ,
haveColor: false
} ,
id: 2 ,
component: "" ,
children: [
{
group: "xxxx" ,
title: "xxx" ,
path: "Business" ,
name: "Business" ,
id: 2 ,
component: ""
} ,
{
group: "变更记录" ,
title: "变更记录" ,
path: "ChangeLog" ,
name: "ChangeLog" ,
id: 2 ,
component: ""
}
]
} ,
{
group: "图谱" ,
title: "图谱" ,
path: "/CompanyFigure" ,
meta: {
icon: "../assets/img/navicon/icon03.png" ,
iconColor: "../assets/img/navicon/icon13.png" ,
haveColor: false
} ,
children: [
{
group: "企业" ,
title: "企业" ,
path: "CompanyChain" ,
name: "CompanyChain" ,
} ,
{
group: "关系" ,
title: "关系" ,
path: "BusinessRelation" ,
name: "BusinessRelation" ,
}
]
} ,
{
group: "资信" ,
title: "资信" ,
path: "/Credit" ,
meta: {
icon: "../assets/img/navicon/icon04.png" ,
iconColor: "../assets/img/navicon/icon14.png" ,
haveColor: false
} ,
children: [
{
group: "aaaa" ,
title: "aaa" ,
path: "Default" ,
name: "Default" ,
} ,
{
group: "公告信息" ,
title: "公告信息" ,
path: "NoticeMsg" ,
name: "NoticeMsg" ,
}
]
} ,
{
group: "集团" ,
title: "集团" ,
path: "/Group" ,
meta: {
icon: "../assets/img/navicon/icon05.png" ,
iconColor: "../assets/img/navicon/icon15.png" ,
haveColor: false
} ,
children: [
{
group: "集团成员" ,
title: "集团成员" ,
path: "GroupMember" ,
name: "GroupMember" ,
} ,
{
group: "新闻舆情" ,
title: "新闻舆情" ,
path: "NewsList" ,
name: "NewsList" ,
}
]
} ,
{
group: "司法" ,
title: "司法" ,
path: "/Judicial" ,
meta: {
icon: "../assets/img/navicon/icon06.png" ,
iconColor: "../assets/img/navicon/icon16.png" ,
haveColor: false
} ,
children: [
{
group: "惩戒" ,
title: "惩戒" ,
path: "Punishment" ,
name: "Punishment" ,
}
]
} ,
{
group: "融资" ,
title: "融资" ,
path: "/FinancingMessage" ,
meta: {
icon: "../assets/img/navicon/icon08.png" ,
iconColor: "../assets/img/navicon/icon18.png" ,
haveColor: false
} ,
children: [
{
group: "融资" ,
title: "融资" ,
path: "AccountsReceivableFinancing" ,
name: "AccountsReceivableFinancing" ,
} ,
{
group: "债券" ,
title: "债券" ,
path: "BondFinancing" ,
name: "BondFinancing" ,
} ,
{
group: "信托" ,
title: "信托" ,
path: "TrustFinancing" ,
name: "TrustFinancing" ,
} ,
{
group: "股权" ,
title: "股权" ,
path: "EquityPledge" ,
name: "EquityPledge" ,
} ,
]
}
] ;
数组扁平化
const flatten = ( arr: any ) => {
let result: any = [ ] ;
for ( let i = 0 ; i < arr. length; i++ ) {
if ( Array . isArray ( arr[ i] . children) ) {
result = result. concat ( flatten ( arr[ i] . children) ) ;
} else {
result. push ( arr[ i] ) ;
}
}
return result;
} ;
扁平化处理后
数组深拷贝
const clone = ( obj: any ) => {
let o: any ;
if ( typeof obj === "object" ) {
if ( obj === null ) {
o = null ;
} else {
if ( obj instanceof Array ) {
o = [ ] ;
for ( let i = 0 , len = obj. length; i < len; i++ ) {
o. push ( clone ( obj[ i] ) ) ;
}
}
else {
o = { } ;
for ( const j in obj) {
o[ j] = clone ( obj[ j] ) ;
}
}
}
} else {
o = obj;
}
return o;
} ;