繁体   English   中英

forEach 和 if 的更好解决方案? map,过滤器 Javascript

[英]Better solution for forEach and if ? map, filter Javascript

对于这个逻辑,我真的需要更好的解决方案:

    this.allClients.forEach(obj => {
     if(obj.status === 2) {
       this.numOfWaitingUsers.push(obj) 
     }
     if(obj.status === 1) {
      this.numOfInactiveUsers.push(obj) 
    }
    if(obj.status === 0) {
      this.numOfActiveUsers.push(obj) 
    }
    if(obj.status === 3) {
      this.numOfAClosedUsers.push(obj) 
    }
    })

这是完美的工作,但我需要更好的解决方案。 我知道用更少的代码可以做得更好。

一种解决方案是 map 从状态编号到数组属性名称,如下所示:

const arrayNameByStatus = {    // This could be an array, but I wasn't sure if
    0: "numOfActiveUsers",     // status codes were necessarily contiguous like
    1: "numOfInactiveUsers",   // they are in the question
    2: "numOfWaitingUsers",
    3: "numOfAClosedUsers",
};
for (const obj of this.allClients) {
    const name = arrayNameByStatus[obj.status];
    if (name) { // Remove this for an error if status is an unexpected value
        this[name].push(obj);
    }
}

现场示例:

 const arrayNameByStatus = { 0: "numOfActiveUsers", 1: "numOfInactiveUsers", 2: "numOfWaitingUsers", 3: "numOfAClosedUsers", }; class Example { constructor() { this.allClients = [ {status: 0}, {status: 2}, {status: 2}, ]; this.numOfActiveUsers = []; this.numOfInactiveUsers = []; this.numOfWaitingUsers = []; this.numOfAClosedUsers = []; } method() { for (const obj of this.allClients) { const name = arrayNameByStatus[obj.status]; if (name) { this[name].push(obj); } } } } const e = new Example(); e.method(); console.log(e);

但是,如果您要定期按状态索引,则可以考虑更改 object 的结构以直接支持它。 例如,您可能有一个userCounts属性,它是一个 object,键为 0 到 3,它可以让您直接索引:

// **IF** you change the structure so that `this` has a `userCounts`
// keyed by status:
for (const obj of this.allClients) {
    const array = this.userCounts[obj.status];
    if (array) { // Remove this for an error if status is an unexpected value
        array.push(obj);
    }
}

现场示例:

 class Example { constructor() { this.allClients = [ {status: 0}, {status: 2}, {status: 2}, ]; this.userCounts = { 0: [], 1: [], 2: [], 3: [], }; } method() { for (const obj of this.allClients) { const array = this.userCounts[obj.status]; if (array) { array.push(obj); } } } } const e = new Example(); e.method(); console.log(e);

老实说,您发布的代码看起来非常好。 但是,如果你有更多的if语句到 go ,你可以像下面这样优化它:

// use the corresponding status as the key to access each array
let dataContainer = {
  0: this.numOfActiveUsers,
  1: this.numOfInactiveUsers,
  2: this.numOfWaitingUsers,
  3: this.numOfAClosedUsers
}
this.allClients.forEach(obj => dataContainer[obj.status].push(obj))

为您的状态代码使用查找表:

// const countNames = {0:"numOfActiveUsers", 1:"numOfInactiveUsers", 2:"numOfWaitingUsers", 3:"numOfAClosedUsers"};
const countNames = ["numOfActiveUsers", "numOfInactiveUsers", "numOfWaitingUsers", "numOfAClosedUsers"];
for (const obj of this.allClients) {
    const count = countNames[obj.status];
    if (count)
        this[count].push(obj) 
}

暂无
暂无

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

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