0

I am trying to figure out a way to add field names and values to an object.

for example I hav ethe following...

$scope.product = {
        "Requirements": [
            {
                "OriginPostcode": '',
                "BearerSize": 100,
                "BandwidthRequired": 10
            }
        ]
   }

And I want to add two more names and values...

 "Term": 36,
 "Quantity": 1

I know the push() function is for arrays. What do you uses for objects?

Many thanks

2
  • 1
    Does this help? stackoverflow.com/questions/22855710/… Commented Feb 10, 2015 at 14:49
  • 2
    That's a JavaScript object. There's no JSON there. Commented Feb 10, 2015 at 14:49

1 Answer 1

2

You can use baces passing index like an array:

  $scope.product["Requirements"][0]["Term"] = 36
  $scope.product["Requirements"][0]["Quantity"] = 1

you can also do in this way

   $scope.product["Requirements"][0].Term = 36
   $scope.product["Requirements"][0].Quantity = 1

and also...

   $scope.product.Requirements[0].Term = 36
   $scope.product.Requirements[0].Quantity = 1

What do you need to understand is how to go through a javascript object using braces [] or point . Take a look at this link

Sign up to request clarification or add additional context in comments.

1 Comment

thank you - the third option worked for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.