2013-01-13 90 views
0

我有一個數據集,看起來像這樣:如何在javascript/jquery中合併兩個json對象?

{ 
"Stops": [ 
    { 
     "Name": "COLUMBIA RD NW + MINTWOOD PL NW", 
     "Routes": [ 
      "42", 
      "43", 
      "H1", 
      "L2" 
     ] 
    } 
] 

}

,另一個是這樣的:

{ 
"Predictions": [ 
    { 
     "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
     "Minutes": 7, 
     "RouteID": "42" 
    }, 
    { 
     "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
     "Minutes": 25, 
     "RouteID": "42" 
    }, 
    { 
     "DirectionText": "North to Chevy Chase Circle", 
     "Minutes": 32, 
     "RouteID": "L2" 
    }, 
    { 
     "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
     "Minutes": 36, 
     "RouteID": "42" 
    }, 
    { 
     "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
     "Minutes": 58, 
     "RouteID": "42" 
    }, 
    { 
     "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
     "Minutes": 69, 
     "RouteID": "42", 
    } 
], 
"StopName": "Columbia Rd Nw + Mintwood Pl Nw" 

}

我想結合有關第一組路線ID的數據,具體添加要設置的分鐘數。所以輸出會是這樣的:

{ 
"Stops": [ 
    { 
     "Name": "COLUMBIA RD NW + MINTWOOD PL NW", 
     "Routes": [ 
      { 
       "42": [ 
        { 
         "Minutes": [7, 25,36,58,69] 
        } 
       ], 
       "43": [ 
        { 
         "Minutes": [] 
        } 
       ], 
       "H1":[ 
        { 
         "Minutes": [] 
        } 
       ], 
       "L2": [ 
        { 
         "Minutes": [32] 
        } 
       ] 
      } 
     ], 
     "StopID": "1001779" 
    } 
] 
} 

我堅持如何寫一些JavaScript/jQuery將做到這一點合併。有任何想法嗎?

+0

[** $擴展()**(http://api.jquery.com/jQuery .extend /) – adeneo

+2

@adeneo我懷疑$ .extend()會完成這項工作。我猜這裏的'for'循環是正確的。 – VisioN

+1

哪裏有數據集?如果它是JSON,那麼你需要以某種方式讀取它們,並將它們解析爲Javascript對象。你需要將結果作爲Javascript對象還是JSON? – Guffa

回答

0

請,這裏是更新後的代碼,空陣列和一切。它是在普通的JavaScript,所以沒有圖書館需要。 它是一個我知道的粗糙的例子,但它的工作原理,我希望它會幫助你找到自己的方式。讓你可以改進它。

var 

    stops = { 
     "Stops": [ 
      { 
       "Lat": 38.92092, 
       "Lon": -77.043684, 
       "Name": "COLUMBIA RD NW + MINTWOOD PL NW", 
       "Routes": [ 
        "42", 
        "43", 
        "H1", 
        "L2" 
       ], 
       "StopID": "1001779" 
      } 
     ] 
    }, 

    predictions = { 
     "Predictions": [ 
      { 
       "DirectionNum": "1", 
       "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
       "Minutes": 7, 
       "RouteID": "42", 
       "VehicleID": "7136" 
      }, 
      { 
       "DirectionNum": "1", 
       "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
       "Minutes": 25, 
       "RouteID": "42", 
       "VehicleID": "7103" 
      }, 
      { 
       "DirectionNum": "0", 
       "DirectionText": "North to Chevy Chase Circle", 
       "Minutes": 32, 
       "RouteID": "L2", 
       "VehicleID": "2207" 
      }, 
      { 
       "DirectionNum": "1", 
       "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
       "Minutes": 36, 
       "RouteID": "42", 
       "VehicleID": "7132" 
      }, 
      { 
       "DirectionNum": "1", 
       "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
       "Minutes": 58, 
       "RouteID": "42", 
       "VehicleID": "7111" 
      }, 
      { 
       "DirectionNum": "1", 
       "DirectionText": "North to Mt Pleasant Via Adams Morgan", 
       "Minutes": 69, 
       "RouteID": "42", 
       "VehicleID": "7135" 
      } 
     ], 
     "StopName": "Columbia Rd Nw + Mintwood Pl Nw" 
    }; 

    var routeTimes = {}; 
    for (var index in predictions.Predictions) { 
     if(!routeTimes.hasOwnProperty(predictions.Predictions[index].RouteID)) { 
      routeTimes[predictions.Predictions[index].RouteID] = []; 
      routeTimes[predictions.Predictions[index].RouteID].push(predictions.Predictions[index].Minutes); 
     } else { 
      routeTimes[predictions.Predictions[index].RouteID].push(predictions.Predictions[index].Minutes); 
     } 
    } 

    var 
     routes = stops.Stops[0].Routes, 
     routesVsMinutes = {}; 

    for(var i in routes) { 
     if (!routesVsMinutes.hasOwnProperty(routes[i])) { 
      routesVsMinutes[routes[i]] = {Minutes: []}; 
     } 
     if (routeTimes[routes[i]]) { 
      routesVsMinutes[routes[i]].Minutes = routeTimes[routes[i]]; 
     } 
    }  
    stops.Stops[0].Routes = routesVsMinutes; 

    console.log(stops); 
+0

謝謝弗拉德,這真的很接近!現在,下一個技巧是添加空行數組,用於在routeTimes中沒有預測的路由 –

+0

@JasonRosenbaum我已經使用更新的代碼示例編輯了我的答案。一探究竟。 –

+0

是的,看起來不錯!非常感謝,這給了項目一個開始。 –

0

您可以使用jQuery map

json1.Stops.routes = $.map(json1.Stops[0].Routes, function(route){ 
    //Take all predictions which concern the route 
    var availables = $.filter(json2.Predictions, function(prediction){ 
    return prediction.RouteID === route 
    }) 
    //Extract all minutes 
    var minutesArray = $.map(availables, function(available){ 
    return available.Minutes 
    }) 
    // Return the object to put in "routes" 
    return {"Minutes": minutesArray} 
}) 

有了:

json1 = {"Stops":[...]} 
json2 = {"Predictions:[...]} 
+1

你有你的代碼錯誤,停止是一個數組,所以它真的應該是json1.Stops [0] .Routes。否則沒有測試你的代碼,但這只是跳到我身上。 –

+0

這是真的!你也可以在數組上做一個映射來添加其他情況,但是我會繼續使用Stops [0] –