2014-04-26 41 views
0

我對JavaScript相當陌生。從我看起來像這樣的SQL Server數據庫retreive數據:在json中返回表格數據的格式

[Object { shortcode="0013A2004031AC9A", latest_measurement=1067, keyid="6801"}, 
Object { shortcode="0013A2004031AC9A", latest_measurement=7, keyid="6802"}, 
Object { shortcode="0013A2004031AC9A", latest_measurement=8598838, keyid="6803"}] 

我想在這樣的JSON格式如下:

{mac : 0013A2004031AC9A, keys : {6801:1067, 6802:7, 6803:8598838}} 

,但我就是不明白這一點。

我有超過以上和每一個新的MAC,我覺得我做的JSON對象

var jsonDataPerMac = {}; 

我循環:

jsonDataPerMac[i]={"mac": device.shortcode, "keys":[]}; 

,但我怎麼填補了鑰匙? 任何提示將不勝感激。 enter code here

var macs = []; 
var jsonDataPerMac = {}; 
var i = 0;    

$.ajax({ 
    url: "/bmmeasurements", 
    type: "GET", 
    data: {"unitid" : unitid}, 
    async: false, 
    success: function (data) { 
    console.log(data); 

    initializeTable(); 

    $.each(data, function (index,device) { 
    //add all distinct macs in an array, to use them as a column header 
    if($.inArray(device.shortcode, macs) == -1) { 
      macs.push(device.shortcode); 
      jsonDataPerMac[i]={"mac": device.shortcode, "keys":[]}; 
      i++; 

      //create a table cell for each possible key. id = 'mac-key' 
      createTableGrid(device.shortcode); 
      } 

      //add the measurement data to the correct cell in the grid 
     $('#' + device.shortcode + '-' + device.keyid).html(device.latest_measurement); 
      }); 
}}); 

回答

0

這是我的主張。我寧願避免使用jQuery來執行這樣簡單的操作。在這個特定的例子中,我們使用forEachfor..in循環。

//new output array 
var newArray = []; 

//we traverse the array received from AJAX call 
array.forEach(function(el) { 
    var added = false; // it's false by default 

    // we check if the mac is already in newArray, if yes - just add the key 
    for(var i in newArray) { 
     if(newArray[i].mac == el.shortcode) { 
      newArray[i].keys.push(el.keyid+":"+el.latest_measurement); 
      added = true; // tells us whether the key has been added or not 
     } 
    } 

    // if key hasn't been added - create a new entry 
    if(!added) { 
     newArray.push({"mac": el.shortcode, "keys":[el.keyid+":"+el.latest_measurement]}); 
    } 
}); 

console.log(newArray); 

您可以將上面的代碼轉換爲函數,然後在您的ajax onSuccess方法中重用它。請記住將數組作爲參數傳遞並返回newArray。

的jsfiddlehttp://jsfiddle.net/2d5Vq/2/

+0

謝謝,這完全符合我的要求。我非常感謝你的幫助。 – Cindy

+0

歡迎您:)您可以隨時將我的答案標記爲有用,這對我來說會是一點回報。 –

+0

畢竟它不完全是我想要做的,但在我的例子中可能不清楚:{mac:0013A2004031AC9A,keys:{「6801」:1067,「6802」:7,「6803」:8598838}} 6801,6082和6803必須是諸如密鑰和mac之類的標識符。我想解決newArray.keys.6801能夠做不同的鍵值之間的計算和比較。 – Cindy

0

您需要首先條目合併...

var reducedData = {}; 
$.each(macs, function(index,macitem){ 
    if (reducedData.hasOwnProperty(macitem.shortcode)) { 
     reducedData[macitem.shortcode].push(macitem.key); 
    } else { 
     reducedData[macitem.shortcode] = [ macitem.key ]; 
    } 
}); 

然後映射到一個數組裏面你想要的格式...

var jsonDataPerMac = [], 
    i = 0; 
$.map(reducedData, function(keys,mac){ 
    jsonDataPerMac[i++] = {"mac": mac, "keys": keys}; 
    // your other code goes here 
}); 

而且你的jsonDataPerMac使用建議你希望它是一個數組。

+0

謝謝您的回覆! – Cindy