-1

クライアント側での JSON データの操作に関する提案が必要です。

JS/jQuery で TYPE1 を TYPE2 に変換する方法に関するヒントはありますか?

これを変換します (TYPE1):

{
    "0": {
        "Field1": "Val1",
        "Field2": "Val2",
        "Field3": "Val3"
    }, 
    "1": {
        "Field1": "Val1",
        "Field2": "Val2",
        "Field3": "Val3"
    }
}

これに (TYPE2):

{
    "SomeName": [
        [
            "Val1",
            "Val3",
            "Val3"],
        [
            "Val4",
            "Val5",
            "Val6"]
    ]
}
4

1 に答える 1

0

これを試して:

var oldObj = {
    "0": {
        "Field1": "Val1",
        "Field2": "Val2",
        "Field3": "Val3"
    }, 
    "1": {
        "Field1": "Val1",
        "Field2": "Val2",
        "Field3": "Val3"
    }
}

var newObj = {"SomeName":[]};           // Initialize a new Object

for(var key in oldObj){                 // Loop through the old item,
    var item = [];                      // Create a temporary variable for each sub-item.
    for(var subKey in oldObj[key]){     // Loop through the sub-item
        item.push(oldObj[key][subKey]); // Push the sub-item's data to the temp var.
    }
    newObj.SomeName.push(item);         // Push the temp var to the new Object.
}
于 2013-02-15T10:28:34.920 に答える