RAHUL DEEP
RAHUL DEEP

Reputation: 655

Adding new Object inside Json at the front

I am having a json object Like this

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

I want to add one more attribute in front so that the JSON will look like

[{"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":""}]

i tried unshift like this-

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

but its giving me result like this...

[{"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":""}]]

Upvotes: 0

Views: 35

Answers (1)

muenchdo
muenchdo

Reputation: 2181

The unshift() method returns the new length of the array, and not the array itself. So you must not reassign your result to $scope.JsonVar. Just use it like this:

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

Upvotes: 1

Related Questions