2015-09-04 64 views
0

添加里面的Json新對象,我有一個JSON對象,像這種在前面

[{"SubLoc":"a","Description":"A","Equipment":""},{"SubLoc":"b","Description":"B","Equipment":""},{"SubLoc":"c","Description":"C","Equipment":""},{"SubLoc":"d","Description":"D","Equipment":""}] 

我要在前面加一個多個屬性,使得JSON看起來像

[{"SubLoc":"Select","Description":"Select","Equipment":""},{"SubLoc":"a","Description":"A","Equipment":""},{"SubLoc":"b","Description":"B","Equipment":""},{"SubLoc":"c","Description":"C","Equipment":""},{"SubLoc":"d","Description":"D","Equipment":""}] 

我試着不印字像這個 -

$scope.JsonVar.unshift({SubLoc:'Select', Description:'Select'}); 

但它給我造成這樣的...

[{"SubLoc":"Select","Description":"Select","Equipment":""}[{"SubLoc":"a","Description":"A","Equipment":""},{"SubLoc":"b","Description":"B","Equipment":""},{"SubLoc":"c","Description":"C","Equipment":""},{"SubLoc":"d","Description":"D","Equipment":""}]] 
+0

你的結果甚至不是有效的JSON,並且在調用'unshift'時有一個表面的'.'。請確保你解決了你的問題中的所有錯別字! – muenchdo

回答

1

unshift()方法返回數組的新長度,而不是數組本身。所以你不能將結果重新分配給$scope.JsonVar。就像這樣使用它:

$scope.JsonVar.unshift({SubLoc:'Select', Description:'Select'}); 
+0

Thnx ..檢查它.. – RHUL

+0

檢查此[JSBin](https://jsbin.com/hobimefixa/1/edit?html,js,output),我發佈的代碼絕對有效。重現JSBin或類似的問題,並將其添加到您的問題。 – muenchdo