2016-07-13 60 views
0

如果數組是一種對象,那麼爲什麼下面的遞歸函數無法輸出嵌套數組?迭代對象時檢查對象與數組

預期產量和實際產量如下所示。

// dummy encode function 
function encode(value) { 
    return value + " (this value was encoded!)" 
} 

var arr = { 
    "title1": { 
     "item1": "one", 
     "item2": "two" 
    }, 
    "title2": "three", 
    "title3": "four", 
    "title4": { 
     "item1": [{ 
      "arrayItem": "First array item" 
     }, { 
      "arrayItem": "Second array item" 
     }] 
    }, 
    "title5": { 
     "item1": { 
      "subItem1": "five", 
      "subItem2": "six" 
     } 
    } 
}; 

function encodeValues(arr) { 
    var keys = Object.keys(arr); 
    for (var i = 0; i < keys.length; i++) { 
     if (!!arr[keys[i]] && typeof arr[keys[i]] === "object") encodeValues(arr[keys[i]]); 

     else arr[keys[i]] = encode(arr[keys[i]]); 
    } 
    return arr; 
} 

var encodedValues = encodeValues(arr); 

console.log(encodedValues) 

預期輸出:

{ 
    title1: { 
     item1: 'one (this value was encoded!)', 
     item2: 'two (this value was encoded!)' 
    }, 
    title2: 'three (this value was encoded!)', 
    title3: 'four (this value was encoded!)', 
    title4: { 
     item1: [{ 
      arrayItem: First array item(this value was encoded!) 
     }, { 
      arrayItem: Second array item(this value was encoded!) 
     }] 
    }, 
    title5: { 
     item1: { 
      subItem1: 'five (this value was encoded!)', 
      subItem2: 'six (this value was encoded!)' 
     } 
    } 
} 

實際輸出:

{ 
    title1: { 
     item1: 'one (this value was encoded!)', 
     item2: 'two (this value was encoded!)' 
    }, 
    title2: 'three (this value was encoded!)', 
    title3: 'four (this value was encoded!)', 
    title4: { 
     item1: [ 
      [Object], 
      [Object] 
     ] 
    }, 
    title5: { 
     item1: { 
      subItem1: 'five (this value was encoded!)', 
      subItem2: 'six (this value was encoded!)' 
     } 
    } 
} 

請問解決方案是沿着以下即行任何檢查陣列的實例if語句? :

if (!!arr[keys[i]] && arr[keys[i]] instanceof Array) 
    encodeValues(arr[keys[i]]); 
else if (!!arr[keys[i]] && typeof arr[keys[i]] === "object") 
    encodeValues(arr[keys[i]]); 
else 
    arr[keys[i]] = encode(arr[keys[i]]); 

謝謝

回答

1

我認爲你的代碼正在做你想要的,但你只是沒有以你期望的格式登錄。

嘗試console.log(JSON.stringify(encodedValues, null, 2));以便您可以在嵌套對象中看到內部。

完整碼(固定佔用相當多的壓痕),包括輸出:

// dummy encode function 
function encode(value) { 
    return value + " (this value was encoded!)" 
} 

var arr = { 
    "title1": { 
    "item1": "one", 
    "item2": "two" 
    }, 
    "title2": "three", 
    "title3": "four", 
    "title4": { 
    "item1": [ 
     { 
     "arrayItem": "First array item" 
     }, 
     { 
     "arrayItem": "Second array item" 
     } 
    ] 
    }, 
    "title5": { 
    "item1": { 
     "subItem1": "five", 
     "subItem2": "six" 
    } 
    } 
}; 

function encodeValues(arr) { 
    var keys = Object.keys(arr); 
    for (var i = 0; i < keys.length; i++) { 
    if (!!arr[keys[i]] && typeof arr[keys[i]] === "object") { 
     encodeValues(arr[keys[i]]); 
    } else { 
     arr[keys[i]] = encode(arr[keys[i]]); 
    } 
    } 
    return arr; 
} 

var encodedValues = encodeValues(arr); 

console.log(JSON.stringify(encodedValues, null, 2)); 

// Output: 
// { 
// "title1": { 
//  "item1": "one (this value was encoded!)", 
//  "item2": "two (this value was encoded!)" 
// }, 
// "title2": "three (this value was encoded!)", 
// "title3": "four (this value was encoded!)", 
// "title4": { 
//  "item1": [ 
//  { 
//   "arrayItem": "First array item (this value was encoded!)" 
//  }, 
//  { 
//   "arrayItem": "Second array item (this value was encoded!)" 
//  } 
//  ] 
// }, 
// "title5": { 
//  "item1": { 
//  "subItem1": "five (this value was encoded!)", 
//  "subItem2": "six (this value was encoded!)" 
//  } 
// } 
// } 
+0

FIP。對,就是那樣。非常感謝您的幫助! – fivedoor