2017-05-08 20 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

相关问题