javascript - Node.js应用程序中的reduce函数省略json json数据并且太麻烦

标签 javascript node.js json reduce

我在客户变量中有这个 json:

{
    "customers": [
        {
            "id": 6,
            "name": "Joe Bloggs",
            "email": "joe@bloggs.com",
            "supplier_id": 1,
            "title": "First Supplier",
            "code": "firstsupplier"
        },
        {
            "id": 7,
            "name": "Paul",
            "email": "paul@hotmail.com",
            "supplier_id": 1,
            "title": "First Supplier",
            "code": "firstsupplier"
        },
        {
            "id": 7,
            "name": "Paul",
            "email": "paul@hotmail.com",
            "supplier_id": 3,
            "title": "New Supplier",
            "code": "new"
        },
        {
            "id": 7,
            "name": "Paul",
            "email": "paul@hotmail.com",
            "supplier_id": 4,
            "title": "PC supplier 3",
            "code": "PC03"
        },
        {
            "id": 8,
            "name": "John Doe",
            "email": "john@doe.com",
            "supplier_id": 1,
            "title": "First Supplier",
            "code": "firstsupplier"
        }
    ]
}

我想更改它,使其看起来像这样:

{
    "customers": [
        {
            "id": 6,
            "name": "Joe Bloggs",
            "email": "joe@bloggs.com",
            "suppliers": [
              {
                "supplier_id": 1,
                "title": "First Supplier",
                "code": "firstsupplier"            
              }
            ]
        },
        {
            "id": 7,
            "name": "Paul",
            "email": "paul@hotmail.com",
            "suppliers": [
              {
                "supplier_id": 1,
                "title": "First Supplier",
                "code": "firstsupplier"
              },
              {
                "supplier_id": 3,
                "title": "New Supplier",
                "code": "new"
              },
              {
                "supplier_id": 4,
                "title": "PC supplier 3",
                "code": "PC03"
              }
            ]
        },
        {
            "id": 8,
            "name": "John Doe",
            "email": "john@doe.com",
            "suppliers": [
              {
                "supplier_id": 1,
                "title": "First Supplier",
                "code": "firstsupplier"            
              }
            ]
        }
    ]
}

我已经设法让其中的一部分单独工作,但我无法让这一切在一个函数中工作。

例如我可以创建 suppliers 数组,但对于客户 ID 7 的情况,数组中仅出现一个 vendor 。

let customers = await query.offset(offset).limit(limit);

customers = customers.reduce((accumulator, item) => {
  if (accumulator[item.id]) {
    const group = accumulator[item.id];
    group.suppliers = [];
    group.suppliers.push({
      id: item.supplier_id,
      code: item.code,
      title: item.title
    });
  } else {
    accumulator[item.id] = item;
    accumulator[item.id].suppliers = [];
    accumulator[item.id].suppliers.push({
      id: item.supplier_id,
      code: item.code,
      title: item.title
    });
    delete accumulator[item.id].supplier_id;
    delete accumulator[item.id].code;
    delete accumulator[item.id].title;
  }
  return accumulator;
}, {});

我的东西看起来也很麻烦。如何在更清晰的函数中获得我想要的结果?

最佳答案

嗨@runnerpaul,在您的代码中一切都很完美,只需删除行 group.suppliers = []; 它将按照您的期望工作

关于javascript - Node.js应用程序中的reduce函数省略json json数据并且太麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56136336/

相关文章:

javascript - 带有 Rails 的 Ajax,附加 div 不起作用

javascript - Javascript 数组对象的行为

android - 如何将 monaca 插件与 monaca localkit 一起使用?

javascript - nodejs 编辑日期 - 意外行为?

javascript - 使用 Json 从服务器加载所有图像,并将它们显示为 Masonry

javascript - window.open 作为选项卡在 Chrome 中不弹出

javascript - elementUI Cascader 中无法正确显示

node.js - 在 Node 异步系列 block 中执行不会等待回调触发

python - json.dump() 将类型从 `dict` 更改为 `NoneType`

php - 如何使用 PHP Mysql 查询获取 mysql 表中的 json 值