实现 通过数据库里一个字段值相等 则把 他合为一条数据

需求: 把红色合为一条数据,绿色合为一条数据

结果: 

不多说直接上代码.........................................................

// 先查所有数据
const goods = await service.goods.findAllGoodsWithParams({
      where: params,
      offset: ctx.pagination.skip,
      limit: ctx.pagination.limit,
    });
// 循环查出数据
    const map = {};
    const temp = [];
    for (let i = 0; i < goods.rows.length; i++) {
// shop_id   是所属店铺字段
      const good = goods.rows[i];
      const key = good.shop_id;
      if (!map[key]) {
        map[key] = good.shop_id;
        temp.push(good);
      }
    }
    ctx.status = 200;
    ctx.body = temp;

  

原文地址:https://www.cnblogs.com/malng/p/10156602.html