<script>
/**
let obj = {username: 'kobe', age: 39, sex: {option1: '男', option2: '女'}};
let obj1 = obj;
obj1.sex.option1 = '不男不女'; // 修改复制的对象会影响原对象
console.log(obj1, obj);
console.log(obj1, obj);
let obj = { username:'kobe'};
let obj2 = Object.assign(obj);
obj2.username = 'wade';
console.log(obj);//{username: "wade"}
*
*/
let newbuild=[ { "id": 1, "name": "estate", "title": "物业管理", "type": "nav", "leave": 1, "order": 1, "parent_id": 0, "url": "/estate" },
{ "id": 2, "name": "house", "title": "费用管理", "type": "nav", "leave": 2, "order": 1, "parent_id": 1, "url": "estate" },
{ "id": 3, "name": "temporaryCharges", "title": "临时收费", "type": "nav", "leave": 3, "order": 1, "parent_id": 2, "url": "charge" },
{ "id": 4, "name": "propertyFees", "title": "物业费", "type": "nav", "leave": 3, "order": 2, "parent_id": 2, "url": "propertyFees" },
{ "id": 6, "name": "propertyFeesEdit", "title": "编辑", "type": "handle", "leave": 4, "order": 2, "parent_id": 4, "url": null },
{ "id": 7, "name": "propertyFeesLogs", "title": "记录", "type": "handle", "leave": 4, "order": 3, "parent_id": 4, "url": null },
{ "id": 8, "name": "propertyFeesPayment", "title": "缴费", "type": "handle", "leave": 4, "order": 4, "parent_id": 4, "url": null },
{ "id": 9, "name": "carFees", "title": "停车费", "type": "nav", "leave": 3, "order": 3, "parent_id": 2, "url": "carFees" },
{ "id": 11, "name": "carFeesLogs", "title": "记录", "type": "handle", "leave": 4, "order": 2, "parent_id": 9, "url": null },
{ "id": 12, "name": "carFeesPayment", "title": "缴费", "type": "handle", "leave": 4, "order": 3, "parent_id": 9, "url": null },
{ "id": 13, "name": "gain", "title": "固定收益", "type": "nav", "leave": 3, "order": 4, "parent_id": 2, "url": "gain" },
{ "id": 14, "name": "bill", "title": "账单管理", "type": "nav", "leave": 2, "order": 2, "parent_id": 1, "url": "estate" },]
function compare(property) {
return function(a, b) {
const value1 = a[property];
const value2 = b[property];
return value1 - value2;// 升序,降序为value2 - value1
};
}
function treedata(data){
// 对源数据深度克隆
const cloneData = JSON.parse(JSON.stringify(data));
// filter嵌套filter相当于for循环嵌套for循环
const result = cloneData.filter(parent => {
// 返回每一项的子级数组
const branchArr = cloneData.filter(child => parent.id === child.parent_id);
// 若子级存在,则给子级排序;且,赋值给父级
if (branchArr.length > 0) {
branchArr.sort(compare('order'));
parent.children = branchArr;
}
// 返回最高的父级,即,parent_id为0,
return parent.parent_id === 0;
});
// 给最高级的父级排序
result.sort(compare('order'));
console.log(result)
}
treedata(newbuild)
let ary = [1, [2, [3, [4, 5]]], 6];
let str = JSON.stringify(ary);
let arr_flat = ary.flat(Infinity);
console.log(arr_flat)
console.log(ary.flat(3))
//递归
/**
*
*
*
*
*/
var arrList = [1,2,3,5,100,500,10000,10000,1000,10000002]
//for循环测试
function forTest(){
console.time("for循环")
for(let i in arrList){
console.log(((arrList[i] + arrList[i]) * 5 - arrList[i])/arrList[i])
}
console.timeEnd("for循环")
}
//递归遍历测试
function recursive() {
console.time("递归遍历")
const testFun = function (i) {
console.log(((arrList[i] + arrList[i]) * 5 - arrList[i])/arrList[i])
if(i == arrList.length - 1){
return
}
i++
testFun(i)
}
testFun(0)
console.timeEnd("递归遍历")
}
forTest()
recursive()
var dataforEach = [
{
name: "所有物品",
children: [
{
name: "水果",
children: [{name: "苹果", children: [{name: '青苹果'}, {name: '红苹果'}]}]
},
{
name: '主食',
children: [
{name: "米饭", children: [{name: '北方米饭'}, {name: '南方米饭'}]}
]
},
{
name: '生活用品',
children: [
{name: "电脑类", children: [{name: '联想电脑'}, {name: '苹果电脑'}]},
{name: "工具类", children: [{name: "锄头"}, {name: "锤子"}]},
{name: "生活用品", children: [{name: "洗发水"}, {name: "沐浴露"}]}
]
}
]
}]
var forFunction = function () {
var str = ""
dataforEach.forEach(function(row){
row.children.forEach(function(row){
row.children.forEach(function(row){
row.children.forEach(function(row){
str += (row.name + ";")
})
})
})
})
console.log(str)
}
forFunction()
var recursiveFunction = function(){
var str = ''
const getStr = function(list){
list.forEach(function(row){
if(row.children){
getStr(row.children)
}else {
str += row.name + ";"
}
})
}
getStr(dataforEach)
console.log(str)
}
recursiveFunction()
</script>
网友评论