清除错误:将此数组“排序”操作移至单独的语句

时间:2018-10-02 10:19:06

标签: angular lint

请使用下面的javascript函数帮助修复掉毛错误。

错误:将此数组“ sort”操作移至单独的语句。

@{  
ViewBag.Title = "Index";
var ID = ViewBag.ID;

3 个答案:

答案 0 :(得分:0)

const empDetail = Object.assign(item, {});

 return empDetail['EmployeeDetails '].sort( (a: any , b: any) => {
    if (a.employeeType < b.employeeType) {
      return 1;
    }
    if (a.employeeType > b.employeeType) {
      return -1;
    }
    return 0;
  });

}

答案 1 :(得分:0)

sort()使数组变异。参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Mutator_methods

因此,请尝试省略赋值运算符:

item.EmployeeDetails.sort((a: IEmployeeDetails, b: IEmployeeDetails) => {
    if (a.employeeType < b.employeeType) {
        return 1;
    }
    if (a.employeeType > b.employeeType) {
        return -1;
    }
    return 0;
});

答案 2 :(得分:-2)

sort()使数组变异。因此,请尝试省略赋值运算符。

item.EmployeeDetails.sort((a: IEmployeeDetails, b: IEmployeeDetails) => {
    if (a.employeeType < b.employeeType) {
        return 1;
    } else if (a.employeeType > b.employeeType) {
        return -1;
    }
    return 0;
});