简体   繁体   English

重构 JavaScript ES6

[英]Refactor Javascript ES6

i need help with refactoring below block of code.我需要帮助重构下面的代码块。 I was asked to avoid using let and to use const, how can i use constant here as i need to return all the options having possible match id.我被要求避免使用 let 并使用 const,我如何在这里使用常量,因为我需要返回所有可能匹配 ID 的选项。

    const findRecordExists = (options, possibleMatchId) => {
  let item;
  options.forEach(option => {
    option.waivers.forEach(waiver => {
      if (waiver.waiverNameId === possibleMatchId) {
        item = option;
      }
    });
  });
  return item;
};

Example of options would be :选项示例如下:

options: [{
  name:Abc
  waivers: [ {waiverNameId :1}, {waiverNameId:2} ]
}]

Use filter to iterate over the options array, returning whether .some of the waiverNameId s match:使用filter来遍历options数组,返回是否.some对的waiverNameId的比赛:

const findRecordExists = (options, possibleMatchId) => {
  return options.filter(
    ({ waivers }) => waivers.some(
      ({ waiverNameId  }) => waiverNameId === possibleMatchId
    )
  );
};

Or, if you don't like destructuring:或者,如果你不喜欢解构:

const findRecordExists = (options, possibleMatchId) => {
  return options.filter(
    option => option.waivers.some(
      wavier => wavier.waiverNameId => waiverNameId === possibleMatchId
    )
  );
};

Since the result is being immediately returned from the findRecordExists function, there isn't even any need for an intermediate item (or items ) variable.由于结果是从findRecordExists函数立即返回的,因此甚至不需要中间item (或items )变量。

That's okay.没关系。

Using const to declare an identifier only makes the value of the identifier unchangeable if the value of the identifier is a JavaScript primitive eg a number or a boolean.如果标识符的值是 JavaScript 原语(例如数字或布尔值),则使用const声明标识符只会使标识符的值不可更改。

If the value of the identifier is an object or an array (an array is a type of object in JavaScript), using const to declare it doesn't mean that the value of that object identifier cannot be changes.如果标识符的值是一个对象或数组(数组是JavaScript中的一种对象类型),使用const声明并不意味着该对象标识符的值不能改变。 It only means that the identifier cannot be reassigned.这仅意味着不能重新分配标识符。

To refactor your code using const , use the code listing below使用 const重构您的代码,请使用下面的代码清单

const findRecordExists = (options, possibleMatchId) => {
  const optionsWithPossibleMatches = [];
  options.forEach(option => {
    option.waivers.forEach(waiver => {
      if (waiver.waiverNameId === possibleMatchId) {
        optionsWithPossibleMatches.push(option);
      }
    });
  });
  return optionsWithPossibleMatches;
};

If you want to skip intermediate steps of creating variables to store each option that matches your condition, you can use the filter method as prescribed by @CertainPerformance如果您想跳过创建变量的中间步骤来存储与您的条件匹配的每个选项,您可以使用@CertainPerformance 规定的filter方法

You can re-factor with using find method.您可以使用find方法重新分解。 This will simplify and avoids the item variable.这将简化并避免item变量。

 const options = [ { name: "Abc", waivers: [{ waiverNameId: 1 }, { waiverNameId: 2 }] } ]; const findRecordExists = (options, possibleMatchId) => options.find(option => option.waivers.find(waiver => waiver.waiverNameId === possibleMatchId) ); console.log(findRecordExists(options, 2)); console.log(findRecordExists(options, 3));

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

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