0

I know this should be very basic , but I am struggling to find answer , spent 5 minutes figuring out how to compose title for this question :)

var response = [{id:1 , name:"Some Name"} , {id:2 , name :"Other Name"}]
var prefixResponse = "users";
var newResponse = {prefixResponse : response }

This will not work as you know , what I need is that newResponse should look like

{users:[{id:1 , name:"Some Name"} , {id:2 , name :"Other Name"}]}

Is this possible to achieve without using JSON striginfy than converting back to JSON

I know it is simple but coming from PHP this is trouble for me :(

2 Answers 2

3

Use bracket notation for variables:

var newResponse = {};
newResponse[prefixResponse] = response
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i knew it was simple !
0

You can use the bracket notation:

var newResponse = {};
newResponse[prefixResponse] = response;

Comments

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.