2017-06-18 77 views
0

喜中排列的對象,這是我的對象數組:更新或創建基於關鍵

notes:Array(3) 
0:Object 
1497752995725: 
"{"id":"1497752995725","top":"470.047px","left":"1130px","text":""}" 

1:Object 
1497753006544:"{"id":"1497753006544","top":"470.047px","left":"1130px","text":"dadssad"}" 

2:Object 
1497753006544: 
"{"id":"1497753006544","top":"325.047px","left":"585px","text":"dadssad"}" 

,我使用的代碼:

var notes = [] 
var obj = { 
     id: sticky.attr('id'), 
     top: sticky.css('top'), 
     left: sticky.css('left'), 
     text: sticky.children('.sticky-content').html() 
     } 

var item = {} 
item[obj.id] = JSON.stringify(obj) 
notes.push(item) 

我想有這種類型輸出的:

notes:Array(3) 
1497752995725: 
"{"id":"1497752995725","top":"470.047px","left":"1130px","text":""}" 
1497753006544: 
"{"id":"1497753006544","top":"470.047px","left":"1130px","text":"dadssad"}" 
1497753006544: 
"{"id":"1497753006544","top":"325.047px","left":"585px","text":"dadssad"}" 

該票據可以是一個數組或對象,以便當鑰匙例如。 1497752995725存在對象的值將被更新,當不是將關鍵字1497752995725與新的身體一起創建。

回答

0

只要保持筆記作爲對象

var notes = {} 

var obj = { 
    id: sticky.attr('id'), 
    top: sticky.css('top'), 
    left: sticky.css('left'), 
    text: sticky.children('.sticky-content').html() 
} 
notes[obj.id] = obj 

它會更新相同的密鑰數據。