2

I have function below and would like to convert parameters to object where parameters same name as key and value

functionA(name, callback, param1, param2, ....paramN)
{
var params = {'param1': param1,.....};
   $.ajax(... data: params.....);
}
nikudale
  • 399
  • 2
  • 12
  • 2
    Using your example, do you mean you'd like the function to return something like: `{name: name, callback: callback, param1: param1, etc.`? – Scott Rudiger Oct 22 '18 at 16:22
  • This question might be related: [How to get function parameter names/values dynamically?](https://stackoverflow.com/q/1007981/1220550) – Peter B Oct 22 '18 at 16:39

4 Answers4

0

My guess based on how this is worded (will edit if further details are provided):

If you're in an environment where ES6 is supported or you can transpile your code via babel or some other transpiler, you could write it like so:

function functionA(name, callback, ...params) {
  const paramsObj = params.reduce((params, param) => {
    return Object.assign(params, {[param]: param});
  }, {});
  $.ajax({data: paramsObj});
}

functionA(null, null, 'a', 'b'); // paramsObj will be: {a: 'a', b: 'b'}

Edit: If instead the OP wants the keys to be param1, param2, etc.:

function functionA(name, callback, ...params) {
  const paramsObj = params.reduce((params, param, i) => {
    return Object.assign(params, {[`param${i + 1}`]: param});
  }, {});
  $.ajax({data: paramsObj});
}

functionA(null, null, 'a', 'b'); // paramsObj will be: {param1: 'a', param2: 'b'}
Scott Rudiger
  • 1,224
  • 12
  • 16
  • The params in both the cases are array. – pritesh Oct 22 '18 at 16:34
  • @pritesh I edited right before your comment, as I realized they were asking for params as an object not an array. – Scott Rudiger Oct 22 '18 at 16:36
  • My bad, it was creating an indexed Object before e.g., `{0: 'a', 1: 'b'}`. Now it creates what I think was asked for: `{a: 'a', b: 'b'}` – Scott Rudiger Oct 22 '18 at 16:59
  • I assume OP wants `{param1: a, param2: b}`, not literally the value of the parameter to be its key. If I pass in `["foo", "bar", "baz"]`, I assume the intent is to produce `{param1: "foo", param2: "bar", param3: "baz"}`, not `{foo: "foo", bar: "bar", baz: "baz}`, which is a completely redundant structure. – user229044 Oct 22 '18 at 17:00
0

One way you can do is

function a(p1, p2, p3) {
    a = {
        [p1]: p1,
        [p2]: p2,
        [p3]: p3,
    }
    console.log(a, 'a')
    // Gives you
    // {a: "a", b: "b", c: "c"}
}

a('a', 'b', 'c')

Combining with Scott Rudiger's Solution, you can do something as

function functionA(name, callback, ...params) {
    var p = params.reduce((obj, item) => {
        return obj[item] = item
    }, {})
    console.log(p)
    // Gives you
    // {dd: "dd", ee: "ee", ff: "ff", gg: "gg"}
}

functionA('pp', 'cc', 'dd', 'ee', 'ff', 'gg')
pritesh
  • 2,162
  • 18
  • 24
0

If i understood correctly, this is what you need.

let fnStr = arguments.callee.toString(),
    argNames = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(/([^\s,]+)/g), //argNames is a list of parameter names
    obj = {};
for(let i= 0; i < arguments.length ; i ++) {
    obj[argNames[i]] = arguments[i]
}
0

Go for rest parameters syntax and Array.reduce() to return object if you support ES6:

function A(name, callback, ...params)
{
  const result =  params.reduce((obj, item,i) => {
     obj["param"+(i+1)] = item;
     return obj;
   }, {})
   console.log(result);
   
}

A("name","callback","param1",2,"whatever","param4");
A("name","callback","param1");

The old way

function A(name, callback, param1)
{
  var params = {};
  var n = 2;
  while( n < Object.keys(arguments).length ){
    params["param"+(n-1)] = arguments[n];
    n++;
  }
  console.log(params);
}

A("name","callback","param1",2,"whatever","param4");
A("name","callback","param1");
scraaappy
  • 2,830
  • 2
  • 19
  • 29