0

I thought this was a javascript problem but I'm not sure.

Say I have an object like this

   var foo = {
        foo1: 'name', 
        foo2: 'name2' 
    }

and when I make a post call with this in angular in the console the post body shows

{foo1: 'name', foo2: 'name2'}

Great, that's all great.

In my backend though let's say the request body needs to be:

{type: {foo1: 'name', foo2: 'name2'}}

Okay, so in angular, I have that type key in $scope.type and the foo object in $scope.foo

I tried to achieve this by just going

$scope.foo = {$scope.type:$scope.foo};

That didn't work, it just said that the '.' in $scope.type wasn't allowed.

So then I tried setting a var type = $scope.type then put

$scope.foo = {type: $scope.foo};

which worked except the response post was the word 'type' and not the actual value itself. What's going on here? why can't I prepend the key $scope.type to my foo object?

1 Answer 1

1

You cannot use dynamic values in an objects braces for the name type. Instead you should do something like this:

var obj = {};
obj[$scope.type] = $scope.foo;
$scope.foo = obj;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I got it working with underscore js to just change the whole object itself so I didn't have to preprend this. But I did try this solution and it worked! Makes sense thanks a lot.

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.