[英]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.!!! 我已经尝试了很多搜索,但是没有运气。
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.