簡體   English   中英

如何遍歷嵌套數據

[英]How to traverse nested data

我必須遍歷大型數據集以收集javascript中節點的最終子元素。

我需要在子“詳細信息”中找到一系列分支代碼。 這是我嘗試獲得的輸出:

[1000、1201、1202、2101、3101、3201]

 { "TITLE": { "FirstLevel": { "Details": { "Code": "01", }, "SecondLevel": [ { "Details": { "Description": "{desc}", }, "ThirdLevel": { "Details": { "Code": "01", }, "FourthLevel": [ { "Details": { "Code": "11", }, "Branch": { "Details": { "Code": "1000", } } }, { "Details": { "Code": "12", }, "Branch": [ { "Details": { "Code": "1201", } }, { "Details": { "Code": "1202", } } ] } ] } }, { "Details": { "Code": "100", }, "ThirdLevel": [ { "Details": { "Code": "02", }, "FourthLevel": { "Details": { "Code": "21" }, "Branch": { "Details": { "Code": "2101", } } } }, { "Details": { "Code": "03", }, "FourthLevel": [ { "Details": { "Code": "31", }, "Branch": { "Details": { "Code": "3101", } } }, { "Details": { "Code": "32", }, "Branch": { "Details": { "Code": "3201", } } } ] } ] } ] } } } 

我已經查看了有關更基本問題的答案,並嘗試調整解決方案。

一種這樣的解決方案采用傳入的id,並將基於該id更新名稱。 我以為我可以在這里使用類似的地圖實現。 這是一個問題,因為數組不僅僅具有“子代”來表示將在哪里存在子節點。

 function update(object, passedId) { object.children.map((element, index) => { if (element.id === passedId) { console.log(index) object.children[index].name = "New Name" } if (element.children != null) { // condition for checking Nesting update(element, passedId) } }) console.log(object.children) } update(obj, "Branch"); 

接下來,我嘗試了一種更簡單的方法

 function getSubItem(obj) { for (item in obj) { for (subItem in obj[item]) { for (subsubItem in obj[subItem]){ console.log(obj[item][subItem][subsubItem]); } } } } getSubItem(obj) 

依此類推,在子項之后的子項之間添加子項,但是這種嵌套的循環看起來非常不穩定,而且由於分支可以嵌套在不同的區域,因此看起來也不可靠。 我在這里錯過了一個簡單的解決方案嗎?

就像是:

function update (data, passedId, acc = []) {
    if (!_.isObject(data)) {
        return acc;
    }

    return _.reduce(_.values(data), (result, item) => {
        result = _.chain(item)
            .get(passedId)
            .thru(val => [val])
            .flatten()
            .compact()
            .map('Details.Code')
            .thru(vals => _.concat(result, vals))
            .value();
       return update(item, passedId, result);
    }, acc);
}

const res = update(data, 'Branch');

嘗試這個:

 const obj = { "TITLE": { "FirstLevel": { "Details": { "Code": "01", }, "SecondLevel": [{ "Details": { "Description": "{desc}", }, "ThirdLevel": { "Details": { "Code": "01", }, "FourthLevel": [{ "Details": { "Code": "11", }, "Branch": { "Details": { "Code": "1000", } } }, { "Details": { "Code": "12", }, "Branch": [{ "Details": { "Code": "1201", } }, { "Details": { "Code": "1202", } } ] } ] } }, { "Details": { "Code": "100", }, "ThirdLevel": [{ "Details": { "Code": "02", }, "FourthLevel": { "Details": { "Code": "21" }, "Branch": { "Details": { "Code": "2101", } } } }, { "Details": { "Code": "03", }, "FourthLevel": [{ "Details": { "Code": "31", }, "Branch": { "Details": { "Code": "3101", } } }, { "Details": { "Code": "32", }, "Branch": { "Details": { "Code": "3201", } } } ] } ] } ] } } }; function transData(data, propertyName, path, result = []) { if (data instanceof Array) { data.forEach(obj => transData(obj, propertyName, path, result)); } else if (data instanceof Object) { let existProperty = Object.keys(data).indexOf(propertyName) > -1; if (existProperty) { getCode(data[propertyName], path, result); } else { Object.keys(data).forEach(key => transData(data[key], propertyName, path, result)); } } function getCode(data, path, result) { if (data instanceof Array) { data.forEach(obj => getCode(obj, path, result)); } else { if (typeof path !== 'undefined') { result.push(path.split(/\\./g).reduce((accumulator, val) => accumulator = accumulator[val], data)); } else { result.push(data); } } } return result; } console.log(transData(obj, 'Branch', 'Details.Code')); console.log(transData(obj, 'Branch')); 

嘗試使用 Deepdash(deep Lodash擴展名)中的_.eachDeep方法。

這是您的情況: https : //codepen.io/yurigor/pen/moMpLV

var res = [];
_.eachDeep(data,function(value,key){
  if(key=="Branch"){
    if(!_.isArray(value)){
      value = [value];
    }
    _.each(value,function(v){
      if(v.Details&&v.Details.Code){
        res.push(v.Details.Code);
      }
    });
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM