2017-07-03 25 views
0

您好,我有一個像這樣分裂基礎上,場中的物體

var x = {code0: "codefdg", mcode0: "mcodefdg", mcode1: "mcodefdg", comments1: "commentsfdg", code3: "fdg"…} 

我想分裂這個對象像這樣

var first = {code0:"codec",mcode0:"microcode"} 
var second = {mcode1: "microcode", comments1:"commencements"} 
var forth ={code3: "fag"} 

對象有沒有什麼辦法來分割這樣的方式? 我嘗試就像把使用循環像

爲(以x對象){} ,但我不知道什麼會寫在這個循環中。

+0

是存在於你的基礎分割的模式?因爲它對我來說非常隨意。 – Nicolas

+0

ya包含相同整數部分的對象將在同一個對象中 –

+0

code0和mcode0具有相同的整數部分(0)。所以我需要這兩個在相同的新對象 –

回答

1

是的,它是可能的,不過,我想輸出中的哈希表:

var input= {code0: "codefdg", mcode0: "mcodefdg", mcode1: "mcodefdg", comments1: "commentsfdg", code3: "fdg"}; 
var output={} 
for(var key in input){ 
var id=+key.substr(-1); 
output[id]=output[id]||{}; 
output[id][key.substr(0,key.length-1)]=input[key]; 
} 

所以輸出中看起來像這樣:

{ 
0:{code,mcode}, 
1:{mcode,comments}, 
3:{code} 
} 
+0

你好。 。和一件事。現在我得到輸出密鑰作爲Code0在輸出我不想這些整數部分我怎麼能刪除它? –

+0

@ mr.cool編輯... –