You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据含重复ID的列表获取对应重复数据库行?

重复ID数组的数据库行匹配问题

假设我有如下ID数组:

[102, 102, 103, 104]

我需要获取数组中每个元素对应的数据库行,比如这个示例里要返回两行ID为102的记录。

我目前使用以下代码,但只能返回去重后的行:

const variants = await this.db.variants.findAll({
    where: {
        variant_id: variant_ids,
    },
    include: {
        model: this.db.products_has_variants,
        as: 'products_has_variants',
        include: {
            model: this.db.products,
            as: 'product',
        },
    },
});

我找到一个可行的实现方案:

const new_variants = variant_ids.flatMap(id => variants.filter(variant => variant.variant_id === id));

内容的提问来源于stack exchange,提问作者Elias Knudsen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 11:47:34