我想改變的財產以後在我的對象的所有值,讓說,例如如何映射對象深入
var a = {a:{c:1},b:2};
desire output : a = {a:{c:5},b:10} //multiply by 5
我做這樣的
var m = function n (o){
return Object.keys(o).map(function(v,i){
if(o[v] !== null && typeof o[v] === 'object') return n(o[v])
else return (o[v]*5);
})
}
a = m({a:{c:1},b:2})
但是,讓
的輸出[
[
5
],
10
]
'map'在數組上工作並返回另一個數組。在你的案例中工作在一個鍵數組上,並返回一個數組(和/或子陣列)。 –
所以我怎麼能實現我的輸出 – Ash
也許檢查這個:https://stackoverflow.com/questions/25333918/js-deep-map-function? – Guntram