简体   繁体   English

如何避免Airbnb ESLint中的“箭头函数期望返回值”错误

[英]How to avoid “Arrow function expect to return a value” error in Airbnb ESLint

I am running eslint and it is recommended to return a value whenever an arrow function(lambda function) is used. 我正在运行eslint,建议每当使用箭头函数(lambda函数)时返回一个值。 Well that makes sense. 那是有道理的。 However, I come across a case that is hard to walk around. 但是,我遇到了一个很难走动的案例。

Dict = {}
Instances = [/* an array of items where items is a dictionary that contains data */]
Instances.map((item) => {
      Dict[item.name] = item.url;
});

My goal is to get the data from the Instances array and fill the dictionary Dict with it. 我的目标是从Instances数组中获取数据并用它填充字典Dict。 I am using the array function to assign key value pair to the dictionary, but that violates the rule of the arrow function. 我正在使用数组函数将键值对分配给字典,但这违反了箭头函数的规则。

Is there any iteratools or functions other than map that would help me to achieve the goal, and avoid the rule violation? 除了map之外是否有任何iteratools或函数可以帮助我实现目标,并避免规则违规?

Edit: This does not adhere to Airbnb's ES6 Style Guide . 编辑:符合Airbnb的ES6风格指南


My goal is to get the data from the Instances array and fill the dictionary with it. 我的目标是从Instances数组中获取数据并用它填充字典。

Use .reduce 使用.reduce

.. and just pass an empty object as the accumulator, filling it up as you iterate through your array. ..并且只是传递一个空对象作为累加器,在迭代数组时将其填满。

 const instances = [ { name: 'foo', url: 'https://google.com' }, { name: 'bar', url: 'https://stackoverflow.com' } ] const result = instances.reduce((dict, item) => { dict[item.name] = item.url return dict }, {}) console.log(result) 

Why not .map ? 为什么不.map

Array.map always returns a new Array and is meant for mapping each array element to another format. Array.map始终返回一个新Array ,用于将每个数组元素映射到另一种格式。

If your resulting data structure shouldn't be an Array , with the same length as the Array you are operating on, you should avoid using it. 如果生成的数据结构不应该是Array ,并且与您正在操作的Array长度相同,则应避免使用它。

Why .reduce instead of .forEach ? 为什么。降低 ,而不是.forEach

I use forEach only for doing "work" rather than transforming data. 我只使用forEach进行“工作”而不是转换数据。 Transforming data is almost always achievable with just map and/or reduce . 仅使用map和/或reduce几乎总是可以实现转换数据。

Here's what I mean by "work": 这就是我所说的“工作”:

const users = [userInstance, userInstance, userInstance]
users.forEach(user => user.sendEmail('Hello World'))

Use forEach instead of map . 使用forEach而不是map

The point of map is to modify each item in an array and put the modified versions in a new array. map点是修改数组中的每个项目并将修改后的版本放在新数组中。

forEach just runs a function on each item. forEach只对每个项目运行一个函数。

If you are looking for ES6 solution to fill dictionary object this could help and should pass ESLint also:- 如果您正在寻找ES6解决方案来填充字典对象,这可能有所帮助,也应该通过ESLint: -

const dict = Instances.reduce((map, obj) => (map[obj.name] = obj.url, map), {});

update 更新

const dict = Instances.reduce((map, obj) => {
  let mapClone = {};
  mapClone = Object.assign({}, map);
  mapClone[obj.name] = obj.url;
  return mapClone;
}, {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM