通过迭代数组将方法分配给对象

时间:2022-06-03 23:59:56

In ES5, I know that it's possible to assign methods to an object using a forEach loop in the following way:

在ES5中,我知道可以使用forEach循环以下列方式为对象分配方法:

var myMethods = [
  {
    name: 'start',
    src: someFn
  },
  {
    name: 'stop',
    src: someOtherFn
  }
];

var myObject = {};

myMethods.forEach(function(method) {
  myObject[method.name] = method.src;
});

In ES2015 (or ES6), is it possible to define these methods in tandem with creating the object? Here is an example of how I might expect this to work:

在ES2015(或ES6)中,是否可以与创建对象一起定义这些方法?以下是我希望如何工作的示例:

// example
const myObject = {
  [...myMethods.map((method) => method.name)]: [...myMethods.map(method) => method.src)]
}

The end result would look like this:

最终结果如下:

const myObject = {
  start: someFn,
  stop: someOtherFn
}

If there is a way to iterate over these methods and assign them to myObject, I would happily restructure the myMethods array so that this is possible.

如果有办法迭代这些方法并将它们分配给myObject,我很乐意重构myMethods数组,以便这是可能的。

The end goal is to be able to assign each of these methods in an external module and not have to duplicate the definition.

最终目标是能够在外部模块中分配这些方法中的每一个,而不必复制定义。

3 个解决方案

#1


5  

Yes, you can use Object.assign and the spread operator in conjunction with computed property names to do

是的,您可以将Object.assign和spread运算符与计算属性名称结合使用

var myObject = Object.assign({}, ...myMethods.map(({name, src}) => ({[name]: src})));

First we map myMethods to an array of little one-property objects, whose key is given by the value of the name property and value by the src property. Then we use the spread operator ... to pass these to Object.assign as parameters. Object.assign then glues them all together for us.

首先,我们将myMethods映射到一个小的一个属性对象的数组,其关键字由name属性的值和src属性的值给出。然后我们使用spread运算符...将这些传递给Object.assign作为参数。然后,Object.assign将它们粘合在一起。

#2


3  

Reduce should do the trick for you. Note that the optional second parameter is used to start with an empty object at the beginning.

减少应该为你做的伎俩。请注意,可选的第二个参数用于从头开始的空对象开始。

var myMethods = [{
  name: 'start',
  src: function() {
    console.log('started')
  }
}, {
  name: 'stop',
  src: function() {
    console.log('stopped')
  }
}];

var myObject = myMethods.reduce((obj, method) => {
  obj[method.name] = method.src;
  return obj;
}, {})


console.log(myObject)
myObject.start()
myObject.stop()

#3


0  

Try assigning to myObject at same line of myMethods assignnemts

尝试在myMethods assignnemts的同一行分配myObject

var myObject = {};

someFn = function(){console.log(this)};
someOtherFn = function(){console.log(this)};

var myObject = {};

someFn = function(){};
someOtherFn = function(){}

var myMethods = [
  {
    name: (myObject["start"] = "start"),
    src: (myObject["start"] = someFn)
  },
  {
    name: (myObject["stop"] = "stop"),
    src: (myObject["stop"] = someOtherFn)
  }
];

#1


5  

Yes, you can use Object.assign and the spread operator in conjunction with computed property names to do

是的,您可以将Object.assign和spread运算符与计算属性名称结合使用

var myObject = Object.assign({}, ...myMethods.map(({name, src}) => ({[name]: src})));

First we map myMethods to an array of little one-property objects, whose key is given by the value of the name property and value by the src property. Then we use the spread operator ... to pass these to Object.assign as parameters. Object.assign then glues them all together for us.

首先,我们将myMethods映射到一个小的一个属性对象的数组,其关键字由name属性的值和src属性的值给出。然后我们使用spread运算符...将这些传递给Object.assign作为参数。然后,Object.assign将它们粘合在一起。

#2


3  

Reduce should do the trick for you. Note that the optional second parameter is used to start with an empty object at the beginning.

减少应该为你做的伎俩。请注意,可选的第二个参数用于从头开始的空对象开始。

var myMethods = [{
  name: 'start',
  src: function() {
    console.log('started')
  }
}, {
  name: 'stop',
  src: function() {
    console.log('stopped')
  }
}];

var myObject = myMethods.reduce((obj, method) => {
  obj[method.name] = method.src;
  return obj;
}, {})


console.log(myObject)
myObject.start()
myObject.stop()

#3


0  

Try assigning to myObject at same line of myMethods assignnemts

尝试在myMethods assignnemts的同一行分配myObject

var myObject = {};

someFn = function(){console.log(this)};
someOtherFn = function(){console.log(this)};

var myObject = {};

someFn = function(){};
someOtherFn = function(){}

var myMethods = [
  {
    name: (myObject["start"] = "start"),
    src: (myObject["start"] = someFn)
  },
  {
    name: (myObject["stop"] = "stop"),
    src: (myObject["stop"] = someOtherFn)
  }
];