简体   繁体   English

通过解构或forEach进行缩减-迭代和Airbnb JavaScript样式指南

[英]Reduce with deconstruction or forEach - Iteration and Airbnb JavaScript Style Guide

I have an object: 我有一个对象:

things = {
  table: 'red',
  chair: 'green'
}

Adhering to Airbnb's JavaScript Style Guide, I want to create a function that duplicates this object, setting all keys of the object to blue. 遵循Airbnb的JavaScript样式指南,我想创建一个复制该对象的函数,将该对象的所有键设置为蓝色。

My 2 options seem to be: 我的2个选项似乎是:

1 Use reduce 1使用reduce

function alwaysBlue(things) {
  return Object.keys(things)
    .reduce((acc, thing) => ({ ...acc, [thing]: 'blue' }), {})
}

2 Use forEach 2 forEach使用

function alwaysBlue(things) {
  const blueThings = {}
  Object.keys(things)
    .forEach(thing => (blueThings[thing] = 'blue'))
  return blueThings
}

(1) deconstruction on every iteration seems expensive, but if I'm to take no-param-reassign in to account I can't just append to the accumulator on each iteration (1)每次迭代的解构似乎很昂贵,但是如果我考虑不使用参数重新分配 ,我就不能在每次迭代时都附加到累加器上

However, forEach (2) should be avoided in favour of map() / every() / filter() / find() / findIndex() / reduce() / some(), according to https://github.com/airbnb/javascript#iterators--nope 但是,根据https://github.com/ ,应避免使用for(2)来支持map()/ every()/ filter()/ find()/ findIndex()/ reduce()/ some() airbnb / javascript#iterators--不

So which is the preferred approach if I'm to adhere to Airbnb's JavaScript Style Guide - or is there another approach I'm missing? 因此,如果我坚持使用Airbnb的JavaScript样式指南,那么哪种方法是首选?或者,我是否缺少其他方法?

I'd say the general guideline would be that you can adhere to your coding conventions of choice unless they force you into writing implementations that have a negative impact on performance that is big enough to consider . 我想说的一般原则是,你能坚持到您选择的编码约定,除非他们强迫你写下来实现,具有足够大考虑对性能产生负面影响。 If that's the case then you will have to make the necessary sacrifice of coding standards in order to write an implementation that performs well enough. 如果真是这样,那么您将不得不牺牲编码标准,才能编写出性能良好的实现。

I do not know what is your real-world data, but I bet #1 would do just fine and if not then #2 would most likely be a good compromise. 我不知道您的真实世界数据是什么,但是我敢打赌,#1会做的很好,否则,#2很可能是一个很好的折衷方案。 The guide doesn't say forEach is bad , it's just not best . 指南并没有说forEach 不好 ,但这不是最好的

Answered on the github issue (which should be the first and only place you go for questions on this guide): github问题上回答(这应该是您在本指南上提出疑问的第一个也是唯一的地方):


Since that's invalid syntax, I assume your object is: 由于这是无效的语法,因此我假设您的对象是:

   things = {
      table: 'red',
      chair: 'green'
    }

Your first example, with the reduce , is correct. 您的第一个示例带有reduce ,是正确的。 "Seems expensive" is something that you shouldn't worry about - performance is the least important thing, only to be concerned about after code is correct, clean, tested, and profiled. “似乎很昂贵”是您不应该担心的事情-性能是最不重要的事情,只有在代码正确,干净,经过测试和配置文件之后才需要担心。

Only if it ends up being necessary (as determined by fully benchmarking your app, not by microbenchmarks like jsperf), then you'd use your forEach approach next, and then if it still was necessary, you'd devolve it to a for loop. 只有在最终有必要(通过对应用程序进行完全基准测试而不是像jsperf这样的微基准测试来确定)时,才可以使用forEach方法,然后,如果仍然有必要,则将其分解for循环。

如果您打算使用mapValues等第三方工具, 可以使用mapValues

_.mapValues(things, () => 'blue');

You can use Object.assign , which doesn't create a new object, but rather just appends the item to the accumulator. 您可以使用Object.assign ,它不会创建新对象,而只是将项目追加到累加器。 Would be better for performance and adheres to the style guide. 性能会更好,并遵循样式指南。

Object.keys(things)
    .reduce((acc, thing) => Object.assign(acc, {[thing]: 'blue' }), {})

Just use a dead simple loop: 只需使用一个简单的死循环:

function alwaysBlue(things) {
  const blueThings = {}
  for (const key in things) // or `for (const key of Object.keys(things))`
    blueThings[key] = 'blue'
  return blueThings
}

Those iteration methods are only good for arrays, which we aren't dealing with here. 这些迭代方法仅适用于数组,在此不做介绍。

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

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