【发布时间】:2019-10-23 15:33:12
【问题描述】:
更新:编辑代码以获得更清晰的解释。
我需要根据另一个属性数组在一个数组中找到 x 个对象,我将使用 for 循环 + lodash.filter 来检索列表。我想知道 NodeJs 是单线程的,它怎么能更有效。一个示例用例是减少 mysql 调用的数量。例如,我有一个产品列表,我想通过他们的产品类别检索一个子集。所以我会得到这样的东西:
const lodash = require('lodash');
...
let productCategories = [{id:111,...},{id:222,...},{id:333,...},{id:444,...}];
let products = await this.getProductByProductCategories(lodash.map(productCategories,'id')); //Does mysql call whereIn ... which I will not include here.
for(let productCategory of productCategories){
let productSubset = lodash.filter(products, {product_category_id: productCategory.id};
//Do something with productSubset
//I also want to reference the parent object here: productCategory.xxx
}
有没有更有效(和代码友好)的方式来执行 for 循环 + lodash.filter 组合?
【问题讨论】:
标签: javascript node.js lodash