-1

I want to insert key & Object in array.

JQuery code:

  var steps = [];
  var RequestParams = {};
  RequestParams.ActionName = 'aname';
  steps.push({ Name:'abc'} ,RequestParams);

My output

"steps": [
            { "Name": "abc" },
            {  
             ActionName :"aname"
             },
  ]

Expected:

"steps": [
            { "Name": "abc",
            "RequestParams": {
             ActionName :"aname"
             },
  ]
Phil Tune
  • 3,154
  • 3
  • 24
  • 46
user3194721
  • 765
  • 4
  • 14
  • 47

2 Answers2

1
steps.push({"Name": "abc", "RequestParams": RequestParams});

What you want to achieve is having inside your steps array a javascript object with a property Name that has value "abc", and a property named RequestParams that has the value of your RequestParamsvariable.

Raibaz
  • 9,280
  • 10
  • 44
  • 65
  • Is it possible without hard coding? – user3194721 Nov 20 '14 at 22:17
  • @user3194721 what do you mean by "hard coding"? What are you using then? – Phil Tune Nov 20 '14 at 22:21
  • 1
    I'd say no, AFAIK javascript doesn't have any reflection mechanism to use the name of the variable inside a script, so you have to hardcode the `RequestParam` name of the property. See also http://stackoverflow.com/questions/9795773/get-variable-name-javascript-reflection – Raibaz Nov 20 '14 at 22:22
0

Should be

steps.push({ Name:'abc' } ,{ RequestParams:RequestParams });
Phil Tune
  • 3,154
  • 3
  • 24
  • 46