2017-09-03 47 views
1

我有這樣如何修剪密鑰的大小在JavaScript對象

var response = [{"2017-04-19T18:00:12+05:30":{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"18:00:12"}},{"2017-04-20T15:00:12+05:30":{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"15:00:12"}}] 

一個JSON對象,我想將其轉換爲這樣的事情

[{"2017-04-19":{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"18:00:12"}},{"2017-04-20":{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"15:00:12"}}] 

我已經試過這

for(var key in response){ 
    if(response.hasOwnProperty(key)){ 
     key = key.substring(0,10); 
    } 
} 

但這並不奏效。我哪裏錯了?

快速編輯: 目前的答案似乎工作,但有一些對象具有相同的日期,但不同的時間。因此,輸出僅顯示一個特定時間的數據。有什麼辦法可以解決這個問題嗎?例如:

var response = [{"2017-04-19T18:00:12+05:30":{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"18:00:12"}},{"2017-04-19T15:00:12+05:30":{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"15:00:12"}}] 

我想這其輸出到像

[{"2017-04-19":[{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"18:00:12"},{"2017-04-19T15:00:12+05:30":{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"15:00:12"}]}] 
+0

你能請張貼的電流輸出,而不是«那並不沒有工作? –

+0

我得到輸入作爲輸出 – Saikiran

回答

1

試試這個獲得元素,這應該解決您的問題

 var response = [{"2017-04-19T18:00:12+05:30":{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"18:00:12"}},{"2017-04-19T17:00:16+05:30":{"command":"008550","device_id": "THUGHGB1", "voltage": "229", "e1": "00.00", "date_time":"18:00:12"}},{"2017-04-20T15:00:12+05:30":{"command":"00","device_id": "THB1", "voltage": "229", "e1": "00.00", "date_time":"15:00:12"}}] 

         var newResponse = []; 
     for (var i = 0; i < response.length; i++) { 
      for (var key in response[i]) { 
       var newKey = key.substring(0, 10); 

       var newObj = {}; 
       if (newResponse.length) { 
        for (var j = 0; j < newResponse.length; j++) { 
         for (var newIdx in newResponse[j]) { 
          if (newIdx === newKey) { 
           newObj[key] = response[i][key]; 
          } else { 
           newObj[newKey] = response[key] 
          } 
         } 
        } 
       } else { 
        newObj[newKey] = response[key] 
       } 
       newResponse.push(newObj) 
      } 
     } 
response = newResponse; 
+0

謝謝你的幫助。但在處理具有相同日期但不同時間的數據時,我遇到了一個問題。編輯該問題以獲取更多細節。請檢查一下 – Saikiran

1

在你的情況下,問題是你指response這是對象的數組。但是,你需要response[0] TI從第0指數

var response = [{ 
 
    "2017-04-19T18:00:12+05:30": { 
 
    "command": "00", 
 
    "device_id": "THB1", 
 
    "voltage": "229", 
 
    "e1": "00.00", 
 
    "date_time": "18:00:12" 
 
    } 
 
}] 
 

 
for (var key in response[0]) { 
 
    if (response[0].hasOwnProperty(key)) { 
 
    // creating substring from key name 
 
    var x = key.substring(0, 10); 
 
    // in same object creating a new key & value using the 
 
    // substring and previous value 
 
    response[0][x] = response[0][key] 
 
    // deleting the old key 
 
    delete response[0][key]; 
 
    } 
 
} 
 
console.log(response)

+0

謝謝你的幫助。但在處理具有相同日期但不同時間的數據時,我遇到了一個問題。編輯該問題以獲取更多細節。請檢查出來 – Saikiran