Javascript方法和局部变量
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Javascript方法和局部变量,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2877字,纯文字阅读大概需要5分钟。
内容图文
![Javascript方法和局部变量](/upload/InfoBanner/zyjiaocheng/781/e3af4d98c958492ebd3b24399d9210d6.jpg)
我一直在搜索互联网,找不到正确的搜索词,所以我最终在这里再次询问Javascript中的所有老年人.
以下代码取自答案on these question
我真的不明白方法或功能是如何工作的.
var list = [{id: 1,title: 'home',parent: null},{id: 2,title: 'about',parent: null},{id: 3,title: 'team',parent: 2},{id: 4,title: 'company',parent: 2} ];
function treeify(list) {
var treeList = [];
var lookup = {};
list.forEach(function(obj) {
obj['children'] = [];
lookup[obj['id']] = obj;
});
console.log(lookup); // [problem number 1]
list.forEach(function(obj) {
if (obj['parent'] != null) {
lookup[obj['parent']]['children'].push(obj);
} else {
treeList.push(obj);
}
});
console.log(treeList); // [problem number 2]
};
treeify(list);
关于问题1:
It resulted an object that had already a children on each parent which is supposedly I think that, parent should have an empty array children at that moment. How does it work? Please enlightened me.
关于问题2
treeList already formed the hierarchy tree. How it happen? it didn’t even push the lookup variable to the treeList variable? It only pushes the obj with a parent that is equal to null (which is the root parent).
希望得到你的答案.
如果你知道任何可以帮助我理解javascript的博客,文章等,请不要犹豫,在你的答案中添加链接.
很多谢谢!
解决方法:
var list = [
{id: 1,title: 'home',parent: null},
{id: 2,title: 'about',parent: null},
{id: 3,title: 'team',parent: 2},
{id: 4,title: 'company',parent: 2}
];
treeify(list);
function treeify(list) {
var treeList = []; //local array
var lookup = {}; //local object
// iterate over each element in list array
list.forEach(function(obj) {
// add a children property to each element in the array
// in this case the children property is an array
obj['children'] = [];
// obj['id'] returns 1,2,3,4
// lookup is an object so we use the id of each element in the list array as a key
// first iteration adds key : 1 and value {id:1, title: 'home', parent: null, children: [] }
// second iteration adds key : 2 and value {id:2, title: 'about', parent: null, children: [] }
// ...
lookup[obj['id']] = obj;
});
/*
console.log(lookup) should output
{
1: {id: 1,title: 'home', parent: null, children: []},
2: {id: 2,title: 'about', parent: null, children: []},
3: {id: 3,title: 'team', parent: 2, children: []},
4: {id: 4,title: 'company', parent: 2, children: []}
}
however, if you run the code the lookup object gets modifyed in
the lines below (lookup[obj['parent']]['children'].push(obj);),
therefore, some items in the lookup object will have children elements in its child array
*/
console.log(lookup); // [problem number 1]
list.forEach(function(obj) {
if (obj['parent'] != null) {
// this line modifyes the lookup object at runtime
// obj has a parent, so we add it to the corresponding parents children array using the id
lookup[obj['parent']]['children'].push(obj);
} else {
//only adds an element to the treelist array if its a parent element
treeList.push(obj);
}
});
console.log(treeList);
};
内容总结
以上是互联网集市为您收集整理的Javascript方法和局部变量全部内容,希望文章能够帮你解决Javascript方法和局部变量所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。