gpt4 book ai didi

javascript - 如何将两个独立变量的结果分配给第三个变量?

转载 作者:行者123 更新时间:2023-11-30 14:02:27 25 4
gpt4 key购买 nike

这里是一个非常微不足道的问题。想不通。
我正在尝试从 primaryWorksecondaryWork 获取结果并将这些结果分配给变量 myWorkList。请让我知道我在这里做错了什么。

谢谢

  let myWorkList
let primaryWork = this.list.filter(r => r.worker === null)
let secondaryWork = this.list.filter(r => r.worker === this.currentWorker.id)
if (this.list) {
if (this.superuser && this.currentWorker) myWorkList = primaryWork && secondaryWork
}
return myWorkList

最佳答案

听起来 primaryWorksecondaryWork 都是数组。您可能正在寻找 .concat() method :

  let myWorkList
let primaryWork = this.list.filter(r => r.worker === null)
let secondaryWork = this.list.filter(r => r.worker === this.currentWorker.id)
if (this.list) {
if (this.superuser && this.currentWorker) myWorkList = primaryWork.concat(secondaryWork)
}
return myWorkList

或者,修复代码中的一些潜在错误:

  // whoever is using the return value from this function expects an array, so if this.list is undefined (or if this.superuser is false) we should return an empty array instead of undefined
let myWorkList = []
// if this.list is undefined, this.list.filter will fail - so we do it inside the conditional block
if (this.list) {
let primaryWork = [];
let secondaryWork = [];

// if this.superuser or this.currentWorker are false, we don't need to waste CPU cycles computing this.list.filter()
if (this.superuser)
// I made the assumption (correct me if I'm wrong) that if r.worker is null, the work belongs to the superuser
primaryWork = this.list.filter(r => r.worker === null)

// if this.currentWorker is undefined, this.currentWorker.id will fail -- so we perform this filter inside yet another conditional block
if (this.currentWorker)
secondaryWork = this.list.filter(r => r.worker === this.currentWorker.id)

myWorkList = primaryWork.concat(secondaryWork)
}
return myWorkList

最后,您可以将所有内容串入一个过滤器,并且只遍历列表一次而不是两次,如下所示:

  return (
// Check that this.list is defined before filtering
this.list ?
this.list.filter(r =>
// superuser case
(this.superuser && r.worker === null)
|| // or
// non-superuser case
(this.currentWorker && r.worker === this.currentWorker.id)
)
// Return an empty array if this.list was undefined
: []
);

请注意,在这个最终版本中,我们不会实例化 myWorkListprimaryWork 次要工作。如果我们可以直接返回我们想要的最终值,我们不需要在内存中分配空数组只是为了稍后对它们进行垃圾回收。这个最终形式的运行速度应该快 2-3 倍:

  • 因为我们将 this.list 数组迭代一次而不是两次,所以速度提高了一倍
  • 比这快一点,因为我们避免了不必要的内存分配

初步基准测试表明它在我的机器上快了大约 2.4:

var list = [{worker: null}, {worker: null}, {worker: 1}, {worker: 2}, {worker: 2}, {worker: 3}, {worker: 4}, {worker: null}, {worker: 2}]

var d0 = new Date(); for (var i = 0; i < 500000; i++) { var primary = list.filter(r => r.worker === null); var secondary = list.filter(r => r.worker === 2); primary.concat(secondary); } console.log(new Date() - d0);
// 659

var d0 = new Date(); for (var i = 0; i < 500000; i++) { list.filter(r => r.worker === null || r.worker === 2); } console.log(new Date() - d0);
// 272

关于javascript - 如何将两个独立变量的结果分配给第三个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com