简体   繁体   English

优化JavaScript

[英]Optimise JavaScript

Can someone please help me to find best way to optimise the below code as it is taking a long time when I have thousand of records searching 有人可以帮我找到优化以下代码的最佳方法,因为当我搜索成千上万条记录时会花费很长时间

var arr =[
   {
      children:[
         {
            children:[
               {
                  children:[
                     {
                        name:'XYZZZZZ'
                     }
                  ]
               }
            ]
         }
      ]
   }
];

        let list = [];

        //Calculate column list
        arr.forEach((obj0) => {
            if (obj0.hasOwnProperty('children')) {
                if (obj0.children.length > 0) {
                    let objchid1 = obj0.children;
                    objchid1.forEach((obj1) => {
                        if (obj1.hasOwnProperty('children')) {
                            if (obj1.children.length > 0) {
                                let objchid2 = obj1.children;
                                objchid2.forEach((obj2) => {
                                    if (obj2.hasOwnProperty('children')) {
                                        if (obj2.children.length > 0) {
                                            let objchid3 = obj2.children;
                                         objchid3.forEach((obj3) => {
                                            if (obj3.name !== 'james') {
                                    console.log('IN THREEE', obj3.name);
                                                list.push(obj3.name);
                                                }
                                            });

                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

I have tried searching a lot but no luck Thanks in advance.!!! 我已经尝试了很多搜索,但是没有运气。

  1. Optimize your data structure. 优化数据结构。 Do not use nested arrays unless you really need to. 除非确实需要,否则不要使用嵌套数组。 NoSQL is so popular when it comes to WebDev because reads happen 100.000 times more than writes and saving on bandwidth (for you and the user) is worth more than saving on duplicate data in a database considering how cheap hardware is 就WebDev而言,NoSQL如此流行,因为考虑到硬件的价格便宜,读取发生的次数比写入的发生次数多100.000倍,并且节省带宽(对您和用户而言)比在数据库中保存重复数据更有价值。
  2. You can save the elements of the deepest array as object keys (with the nested .name attribute in your case) and the index of the respective position in the array as the object value. 您可以将最深数组的元素另存为对象键(在您的情况下为嵌套的.name属性),并将数组中各个位置的索引作为对象值。 This way you can do myArray[myElementsToIndexObject['elementIamLookingFor']] iterating only one single time over the nested array (for building myElementsToIndexObject ) 这样,您可以对嵌套数组进行一次myArray[myElementsToIndexObject['elementIamLookingFor']]迭代(用于构建myElementsToIndexObject

If the data is from a JSON string, the search can be done during the parsing : 如果数据来自JSON字符串,则可以在解析期间进行搜索:

 var list = [], json = '[{"child":[{"child":[{"child":[{"name":"XYZZZZZ"}]}]}]}]' var arr = JSON.parse(json, (key, val) => (key === 'name' && list.push(val), val)) console.log(list) console.log(arr) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM