【问题标题】:flattening sequelize query result展平 sequelize 查询结果
【发布时间】:2022-08-19 06:19:26
【问题描述】:

我想展平这个查找查询结果。这是一个例子。

\"uuid\": \"5d7c5571-9f39-45ba-93d5-27f91c3322d8\",
        \"type\": \"A\",
        \"number\": \"00001\",
        \"code\": \"978020137962\",
        \"matches\": null,
        \"title\": \"Generate Free Barcodes\\n\",
        \"remark\": \"\",
        \"detail\": null,
        \"minStock\": \"0.000000\",
        \"freeStock\": \"5.000000\",
        \"realStock\": \"20.000000\",
        \"targetStock\": \"0.000000\",
        \"price\": \"156.45\",
        \"minPrice\": \"85.37\",
        \"recommendedPrice\": null,
        \"latestPrice\": \"5.00\",
        \"averagePrice\": \"51.26\",
        \"bulkPrices\": null,
        \"priceUnit\": \"1.000000\",
        \"packageUnit\": \"1.000000\",
        \"stockManagement\": true,
        \"createdOn\": \"2022-07-17T18:23:41.623Z\",
        \"modifiedOn\": null,
        \"unit\": {
            \"uuid\": \"c5327983-bc02-4350-bb45-cd3abbd2eb7d\",
            \"group\": \"Length\",
            \"name\": \"Foot\",
            \"abbreviation\": \"ft\"
        },
        \"bundleChildren\": [],
        \"productionChildren\": [],
        \"bundleParents\": [
            {
                \"quantity\": \"12.000000\",
                \"remark\": \"Testing child remark\",
                \"bundleParent\": {
                    \"uuid\": \"80b82a5a-de4b-4798-a4e6-7e3064edae91\",
                    \"type\": \"B\",
                    \"number\": \"AminB1\",
                    \"code\": \"21\",
                    \"unit\": {
                        \"name\": \"Meter\",
                        \"group\": \"Length\",
                        \"abbreviation\": \"m\"
                    }
                }
            }
        ],

结果如下: \"bundleParents\": [ { \"uuid\": \"80b82a5a-de4b-4798-a4e6-7e3064edae91\", \"数量\": \"12.000000\", \"remark\": \"测试子备注\", \"类型\": \"B\", \"数字\": \"AminB1\", \"代码\": \"21\", \"单位\": \"米\", \"缩写\": \"m\" } ],

这是我在 sequelize 中的查询:

Product.findAndCountAll({
  where: condition,
  order: [[\'createdOn\', \'DESC\']],
  limit: getLimit(req),
  offset: getOffset(req),
  attributes: {exclude: [\'unitId\']},
  include: [
    {model: Unit, as: \'unit\', attributes: {exclude: [\'createdOn\', \'modifiedOn\']}},
    {model: BundleElement, as: \'bundleChildren\', attributes: [\"quantity\", \"remark\"], },

    {model: ProductionElement, as: \'productionChildren\', attributes: [\"quantity\", \"remark\"]},
    {model: BundleElement, as: \'bundleParents\', attributes: [\"quantity\", \"remark\"],
      include: [{
        model: Product, as: \"bundleParent\", attributes: [\'uuid\', \'type\', \'number\', \'code\'],
        include: [{model: Unit, as: \'unit\', attributes: [\'name\', \'group\', \'abbreviation\']}]
      }]
    },
    {model: ProductionElement, as: \'productionParents\', attributes: [\"quantity\", \"remark\"]},
  ],
  distinct:true
})

谁能帮我这个?

    标签: node.js sequelize.js


    【解决方案1】:

    您应该只简化查询,删除不需要的属性。不过,我不会尝试在查询中进行转换(展平)。

    获得结果后,只需提取所需的字段并将它们重新映射到新对象即可。

    // e.g. extract the bundleParents element
    const bundleParents = [
    {
      "quantity": "12.000000",
      "remark": "Testing child remark",
      "bundleParent": {
        "uuid": "80b82a5a-de4b-4798-a4e6-7e3064edae91",
        "type": "B",
        "number": "AminB1",
        "code": "21",
        "unit": {
          "name": "Meter",
          "group": "Length",
          "abbreviation": "m"
        }
      }
    }];
    
    // and then just remap it to flat objects
    const flat = bundleParents.map((bp) => {
      const { quantity, remark, bundleParent } = bp;
      const { unit: { name: unit, abbreviation }, ...base } = bundleParent;
      return {...base, unit, abbreviation, quantity, remark};
    });
    
    console.log(flat);

    【讨论】:

    • 感谢您的回答。这不是我想要的确切解决方案。但我找到了我在下面发布的解决方案。
    【解决方案2】:

    我找到了解决方案,所以我把它贴在这里,也许它对其他人也有帮助。

    flatArray = (array) => {
          return array.map(it => {
            return Object.keys(it.toJSON()).reduce((object, k) => {
              if (!Array.isArray(it[k])) {
                if (typeof it[k] === "object" && it[k] !== null) {
                  Object.keys(it[k].toJSON()).forEach(key => {
                    object[key] = it[k][key]
                  });
                } else object[k] = it[k];
              } else object[k] = flatArray(it[k]);
              return object;
            }, {});
          });
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-14
      • 2019-07-07
      • 2019-12-17
      • 2012-09-23
      • 2012-10-28
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      相关资源
      最近更新 更多