Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
haoban-3
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
office
haoban-3
Commits
d399210e
Commit
d399210e
authored
Oct 13, 2022
by
Jings
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add: 公共方法添加
parent
371e935f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
0 deletions
+68
-0
index.js
src/utils/index.js
+68
-0
No files found.
src/utils/index.js
View file @
d399210e
...
...
@@ -542,4 +542,71 @@ export function initDataRange() {
let
startDay
=
startDate
.
getDate
()
<
10
?
'0'
+
startDate
.
getDate
()
:
startDate
.
getDate
();
let
startNewMonth
=
startMonth
<
10
?
'0'
+
startMonth
:
startMonth
;
return
[
`
${
startYear
}
-
${
startNewMonth
}
-
${
startDay
}
`
,
`
${
year
}
-
${
newMonth
}
-
${
day
}
`
];
}
/**
* 将平铺的数据结构转为树形结构
* @param {*} list 数组
* @param {*} rootValue 根元素id
* @param {*} param2 可配置参数
* @returns
*/
export
function
arrToTree
(
list
,
rootValue
,{
idName
=
'id'
,
parentIdName
=
'parentId'
,
childName
=
'children'
}
=
{})
{
const
objMap
=
{};
// 暂存数组以id为key的映射关系
const
result
=
[];
// 结果
for
(
const
item
of
list
)
{
const
id
=
item
[
idName
];
const
parentId
=
item
[
parentIdName
];
// 该元素有可能已经放入map中(找不到该项的parent时会先放入map)
objMap
[
id
]
=
!
objMap
[
id
]
?
item
:
{...
item
,...
objMap
[
id
]}
const
treeItem
=
objMap
[
id
];
// 找到映射的那一项 引用
if
(
parentId
===
rootValue
)
{
// 根元素 将结果放入
result
.
push
(
treeItem
)
}
else
{
// 若父元素不存在,初始化父元素
if
(
!
objMap
[
parentId
])
{
objMap
[
parentId
]
=
{}
}
// 若无该跟元素则放入map中
if
(
!
objMap
[
parentId
][
childName
])
{
objMap
[
parentId
][
childName
]
=
[]
}
objMap
[
parentId
][
childName
].
push
(
treeItem
)
}
}
return
result
}
export
function
getArrDifference
(
arr1
,
arr2
)
{
let
result
=
[];
arr1
.
forEach
(
item
=>
{
if
(
arr1
.
includes
(
item
)
&&
!
arr2
.
includes
(
item
))
{
result
.
push
(
item
)
}
})
return
result
;
}
// 过滤删除和新增
export
function
getDelOrAddDate
(
arr1
,
arr2
)
{
var
arr
=
[];
var
bool
=
false
;
for
(
var
i
=
0
;
i
<
arr1
.
length
;
i
++
){
for
(
var
j
=
0
;
j
<
arr2
.
length
;
j
++
){
//进行优化遇到相同直接跳出循环 同时支持对象比对
if
(
JSON
.
stringify
(
arr1
[
i
])
===
JSON
.
stringify
(
arr2
[
j
])){
bool
=
false
;
break
;
}
else
{
bool
=
i
;
}
}
if
(
bool
!==
false
)
arr
.
push
(
arr1
[
bool
]);
}
return
arr
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment