0

I'm trying to find a way to make the following possible with NodeJS.

a = 10
b = 20
c = 30
d = 40
......
......

function getObject(a, b, c, d, ....) => {

    // this function should return an object.
    // return {
          a : 10,
          b : 20,
          c : 30,
          d : 40,
          .......
          .......
        }
   }

Is there a way to do it in javascript (NodeJS)?

[Edit - Solved]

As suggested by @Offirmo in this thread and @MarkMeyer in the comments, the problem in the question can be solved with ES6 object notation.

let a = 10;
let b = 20;

function getOject(data){
    console.log(data)
}

getObject({a,b}) // { a: 10, b: 20 }
Joy
  • 45
  • 1
  • 9
  • How will you call the function? – Mark Aug 26 '18 at 01:36
  • The function will be called with n number or arguments (variables). `getObject(a, b, c, d, ......)` – Joy Aug 26 '18 at 01:51
  • 1
    So if you can have code that calls `obj = getObject(a, b, c, d, ......)` why don't you just use `obj = {a, c, b, d}` and make the object that way? Why do you need a function? – Mark Aug 26 '18 at 01:53
  • So basically I want something like, nameOfVariable : valueOfVariable pair. – Joy Aug 26 '18 at 01:54
  • Right, that's what `obj = {a, c, b, d}` does. – Mark Aug 26 '18 at 01:55
  • Instead of creating the object manually for n-number of variables, I want to generate it dynamically by just using the variable. – Joy Aug 26 '18 at 01:56
  • Oh! never mind, I see what you are saying. Wow! never thought it's that easy. – Joy Aug 26 '18 at 01:58
  • Would you mind adding it as an answer, I'll accept it. – Joy Aug 26 '18 at 01:59
  • In that case this question is a dupe of: https://stackoverflow.com/questions/3404057/determine-original-name-of-variable-after-its-passed-to-a-function and @MarkMeyer's solution is a dupe of @ Offirmo's solution there – duhaime Aug 26 '18 at 01:59
  • I think you're right @duhaime – Mark Aug 26 '18 at 02:02
  • @duhaime, Thanks for pointing that out, that question didn't pop up for my search queries. – Joy Aug 26 '18 at 02:03

4 Answers4

1

Solution 1:

It should be as simple as this :

function test(a,b,c,d){
  return {a,b,c,d}
}

let result= test(1,2,3,4)
console.log(result)

Solution 2:

Generic Method for getting the parameters object, in any function... Should work ... but it uses arguments.callee which is deprecated since ECMAScript 5 ( however it still does work in most major browsers.)

function getArgumentsObject(fn,arg){
  let STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
  let ARGUMENT_NAMES = /([^\s,]+)/g;  
  let fnStr = fn.toString().replace(STRIP_COMMENTS, '');
  let names = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
  if(names === null) na,es = [];
     
  let obj = {};
  for(let a=0; a<Array.from(arg).length; a++){
    obj[names[a]]=arg[a]
  }
  return obj;
}

function test(a,b,c,d){
  return getArgumentsObject(arguments.callee, arguments);
}

let result = test(1,2,3,4)
console.log(result)
colxi
  • 7,640
  • 2
  • 45
  • 43
1

As suggested by @Offirmo in this thread and @MarkMeyer in the comments, the problem in the question can be solved with ES6 object notation.

let a = 10;
let b = 20;

function getOject(data){
     console.log(data)
}

getObject({a,b}) // { a: 10, b: 20 }
Joy
  • 45
  • 1
  • 9
1

Nope, once you pass the values to a function, the function can not access the variable names. Keep in mind, a value may be passed directly without a variable name.

const getObject = (a, b, c, d) => ({a, b, c, d})
const foo = 1;
const obj = getObject(foo, 2, 3, 4};
console.log(obj); // {a: 1, b: 2, c: 3 d: 4}

In es6+ you can create a new object like this Mark Meyer states:

const obj = {a, b, c, d};
Mke Spa Guy
  • 793
  • 4
  • 13
0

I propose to make use of the arguments object. We convert arguments to an array, and then we build an Object, based on the output. To get keys in alphabetic order, we'd use String.fromCharCode().

const foo = function() {
  const _args = [].slice.call(arguments).reduce((obj, key, index) => ({
    ...obj,
    [String.fromCharCode(97 + index)]: arguments[index]
  }), {})

  return _args // { a: 1, b: "hello", c: 2, d: 3, e: "world" }
}

foo(1, 'hello', 2, 3, 'world')