Javascript 如何将函数参数转换为键/值对?

Javascript 如何将函数参数转换为键/值对?,javascript,node.js,Javascript,Node.js,我正试图找到一种方法,通过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,

我正试图找到一种方法,通过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,
          .......
          .......
        }
   }
有没有一种用javascript(NodeJS)实现的方法

[编辑-已解决]


正如@Offirmo in和@MarkMeyer在评论中所建议的,问题中的问题可以用ES6对象表示法解决

let a = 10;
let b = 20;

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

getObject({a,b}) // { a: 10, b: 20 }
let a = 10;
let b = 20;

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

getObject({a,b}) // { a: 10, b: 20 }
解决方案1: 它应该是这么简单:

功能测试(a、b、c、d){
返回{a,b,c,d}
}
让结果=试验(1,2,3,4)
console.log(结果)
解决方案1: 它应该是这么简单:

功能测试(a、b、c、d){
返回{a,b,c,d}
}
让结果=试验(1,2,3,4)

console.log(result)
正如@Offirmo in和@MarkMeyer在评论中所建议的,问题中的问题可以用ES6对象表示法解决

let a = 10;
let b = 20;

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

getObject({a,b}) // { a: 10, b: 20 }
let a = 10;
let b = 20;

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

getObject({a,b}) // { a: 10, b: 20 }

正如@Offirmo in和@MarkMeyer在评论中所建议的,问题中的问题可以用ES6对象表示法解决

let a = 10;
let b = 20;

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

getObject({a,b}) // { a: 10, b: 20 }
let a = 10;
let b = 20;

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

getObject({a,b}) // { a: 10, b: 20 }

不,一旦将值传递给函数,该函数就无法访问变量名。请记住,可以不使用变量名直接传递值

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}
在es6+中,您可以创建一个新对象,如Mark Meyer所述:

const obj = {a, b, c, d};

不,一旦将值传递给函数,该函数就无法访问变量名。请记住,可以不使用变量名直接传递值

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}
在es6+中,您可以创建一个新对象,如Mark Meyer所述:

const obj = {a, b, c, d};

我建议使用
参数
对象。我们将
参数
转换为数组,然后根据输出构建一个对象。要按字母顺序获取键,我们将使用
String.fromCharCode()


我建议使用
参数
对象。我们将
参数
转换为数组,然后根据输出构建一个对象。要按字母顺序获取键,我们将使用
String.fromCharCode()


如何调用该函数?将使用n个数字或参数(变量)调用该函数
getObject(a,b,c,d,…)
那么,如果您可以使用调用
obj=getObject(a,b,c,d,…)
的代码,为什么不直接使用
obj={a,c,b,d}
并将对象变成那样呢?为什么需要一个函数?所以基本上我想要的是,nameOfVariable:valueOfVariable pair。对,这就是
obj={a,c,b,d}
所做的。你将如何调用函数?函数将使用n个数或参数(变量)调用
getObject(a,b,c,d,…)
那么,如果您可以使用调用
obj=getObject(a,b,c,d,…)
的代码,为什么不直接使用
obj={a,c,b,d}
并将对象变成那样呢?为什么需要一个函数?所以基本上我想要一个类似,nameOfVariable:valueOfVariable pair的东西。对,这就是
obj={a,c,b,d}
所做的。