2017-05-08 49 views
3

假設我有這樣的對象:如何改變的最深刻的對象爲一個字符串值,如果它是在JavaScript空對象

{CONN_INFO: {CFGSwitch: {412: {}}}} 

它已爲4的深度我要重新分配的關鍵412"{}"這種用於檢查深度最終對象將是

{CONN_INFO: {CFGSwitch: {412: "{}"}}} 

功能:

function checkDepth(object) { 
    var level = 1; 
    var key; 
    for(key in object) { 
     if (!object.hasOwnProperty(key)) continue; 

     if(typeof object[key] == 'object'){ 
      var depth = checkDepth(object[key]) + 1; 
      level = Math.max(depth, level); 
     } 
    } 
    return level; 
} 

我有這樣的東西,但我不確定它是最佳的還是適用於所有情況。將不勝感激任何投入。

function checkIfLastLevelEmpty(obj, depth) { 
    for (var key in obj) { 
    var val = obj[key]; 
    if (Object.keys(obj[key]).length === 0) { 
     obj[key] = "{}"; 
    } 
    else { 
     if (depth >0) { 
     checkIfLastLevelEmpty(val, depth-1); 
     } 
    } 
    } 
} 
+0

也許這可以幫助你http://stackoverflow.com/questions/13523951/how-to-check-the-depth-of-an-object – b2ok

+0

在你的checkIfLastLevelEmpty函數中,深度的目的是什麼'變量,是否提供最大深度限制? –

+0

@Blue是的,我對遞歸函數不太熟悉,但是如果深度爲0並且沒有空對象,我將使用深度變量作爲出口。我已將當前的代碼更新爲原始問題。 – tchan

回答

1

首先讓一些結合知識:

How do I test for an empty JavaScript object?

對上述問題有它很多的選擇,我是一個真棒答案將編輯片段以形成幾個功能:

function isObject (obj) { 
    return obj.constructor === Object; 
} 

function isEmptyObject (obj) {  
    return Object.keys(obj).length === 0 && isObject(obj); 
} 

Ace。現在,假設你有一個非常線性的對象結構,它不是一個複雜的數組,並且不關心深度(只有它是空的)才能讓一個函數在樹中循環。

function convertEmptyObjects (obj, currentDepth) { 
    currentDepth = currentDepth || 0; 
    for (var key in obj) { 
     var val = obj[key]; 
     if (isObject(val)) { 
      if (isEmptyObject(val)) { 
       obj[key] = "{}"; 
       console.log("Defeated boss ("+ key +") on level "+ currentDepth +"!"); 
      }else{ 
       convertDeepestEmptyObject (val, currentDepth + 1);      
      } 
     } 
    } 
    return obj; 
} 

讓一個可怕的尋找對象上測試:你有它

var testObject = { 
    CONN_INFO: { 
     CFGSwitch: { 
      412: {}, // Level 2 
      413: { 
       content: {} // Level 3 
      } 
     }, 
     DummySwitch: {} // Level 1 
    }, 
    TEST_CONN_INFO: { 
     CFGSwitch: { 
      414: {}, // Level 2 
      415: { 
       content: { 
        host: "google", 
        port: "8080", 
        protocol: {} // Level 4 
       } 
      } 
     } 
    }, 
    STAGING_CONN_INFO: {} // Level 0 
} 

convertEmptyObjects(testObject); 
JSON.stringify(testObject) 

/* Output: 
* Defeated boss (412) on level 2! 
* Defeated boss (content) on level 3! 
* Defeated boss (DummySwitch) on level 1! 
* Defeated boss (414) on level 2! 
* Defeated boss (protocol) on level 4! 
* Defeated boss (STAGING_CONN_INFO) on level 0! 
*/ 

// Result: 
{ 
    "CONN_INFO": { 
     "CFGSwitch": { 
      "412": "{}", // Empty String {} 
      "413": { 
       "content": "{}" // Empty String {} 
      } 
     }, 
     "DummySwitch": "{}" // Empty String {} 
    }, 
    "TEST_CONN_INFO": { 
     "CFGSwitch": { 
      "414": "{}", // Empty String {} 
      "415": { 
       "content": { 
        "host": "google", 
        "port": "8080", 
        "protocol": "{}" // Empty String {} 
       } 
      } 
     } 
    }, 
    "STAGING_CONN_INFO": "{}" // Empty String {} 
} 

1
deeper=obj=>(Object.keys(obj[Object.keys(obj)[0]]).length&&deeper(obj[Object.keys(obj)[0]])&&true)||(obj[Object.keys(obj)[0]]="{}"); 
var obj={CONN_INFO: {CFGSwitch: {412: {}}}}; 
deeper(obj); 
console.log(obj); 

http://jsbin.com/nidahuhema/edit?console

你爲什麼要檢查的深度,然後進入呢?你不能一氣呵成嗎?

上碼的長型:

function deeper(obj){ 
    if(Object.keys(Object.values(obj)[0]).length){ // if the child object has properties 
    return deeper(Object.values(obj)[0]); 
    } 
    obj[Object.keys(obj)[0]]="{}"; 
} 
+0

我檢查深度,因爲這隻發生在第三級之後。如果我將遞歸函數應用於表格中的所有數據,它只會凍結我的瀏覽器。編寫遞歸函數會讓我的頭腦轉動,但是這個解決方案對我來說很合適,但是,謝謝:) – tchan

+0

雖然函數的問題是,如果沒有空對象,例如,如果我將空對象更改爲字符串'var obj = {CONN_INFO:{CFGSwitch:{412:「1」}}};'你將得到一個超出RangeError:maxium調用堆棧大小 – tchan

相關問題