0

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":""}]]
1
  • Your result is not even valid JSON, and there is a superficial . in your call to unshift. Please make sure you fix all the typos in your question! Commented Sep 4, 2015 at 16:25

1 Answer 1

1

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'});
Sign up to request clarification or add additional context in comments.

1 Comment

Check this JSBin, the code I posted definitely works. Reproduce your problem in a JSBin or similar and add it to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.