【问题标题】:How to filter and grouped by id nested data in javascript?如何在javascript中按id嵌套数据过滤和分组?
【发布时间】:2020-11-05 01:44:09
【问题描述】:

我有 JSON 数据,其结构如下:

const SubSpecs= [
        {
            "_id": "1",
            "nom": "installation",
            "blocks": [
                {
                    "idBlock": "1",
                    "ArtisanOffres": [
                        {
                            "idArtisan": "1",
                            "price": 12
                        }
                    ]
                },
                {
                    "idBlock": "2",
                    "ArtisanOffres": [
                        {
                            "idArtisan": "1",
                            "price": 14
                        }
                    ]
                }
            ]
        },
        {
            "_id": "2",
            "nom": "tech",
            "blocks": [
                {
                    "idBlock": "1",
                    "ArtisanOffres": [
                        {
                            "idArtisan": "1",
                            "price": 12
                        }
                    ]
                },
                {
                    "idBlock": "2",
                    "ArtisanOffres": [
                        {
                            "idArtisan": "1",
                            "price": 14
                        },
                        {
                            "idArtisan": "2",
                            "price": 50
                        }
                    ]
                }
            ]
        }
        
    ],
   
}

这是我想通过其 id 过滤的工匠数组:

 artisans= [
        {
            "_id": "1",
            "username": "test1"
        },
        {
            "_id": "2",
            "username": "test2"
        }
    ]

现在我想提取所有具有相同工匠 id 的价格(按 id 提取)并按此 id 分组!!

我刚开始一个简单的功能,但我没有找到完成它的方法! 谁能帮忙!!

我无法理解如何过滤此类数据?下面的代码不起作用我找不到任何此类过滤的示例。

这是我的功能:

const total = data
        .map((fb) =>
          fb.blocks
            .map((b) =>
              b.ArtisanOffres.map((ao)=> ao.price)
                .join(",")
                .includes("1")
            )
            .join(",")
        )
        .join(",")
        .split(",");

我是 javascript 新手,遇到了这个算法问题,

【问题讨论】:

  • 您不能在 JavaScript 中映射 {}。请修改您的data.map 示例。
  • 请同时添加想要的结果。
  • @Enijar 我已经修改了我的代码
  • username 来自哪里?
  • 但是includes 不会帮助您按 id 分组,请尝试使用reduce 或在您的地图函数中使用自定义累加器变量。啊啊,函数式编程的趋势,你对年轻人做了什么?带有结果变量的基本循环会如此简单!

标签: javascript node.js arrays json reactjs


【解决方案1】:

Ciao,你觉得这个解决方案怎么样?我通过 idArtisan === "1" 过滤了您的数据,然后将所有价格相加。结果是 52。这里的工作示例:

const SubSpecs= [
        {
            "_id": "1",
            "nom": "installation",
            "blocks": [
                {
                    "idBlock": "1",
                    "ArtisanOffres": [
                        {
                            "idArtisan": "1",
                            "price": 12
                        }
                    ]
                },
                {
                    "idBlock": "2",
                    "ArtisanOffres": [
                        {
                            "idArtisan": "1",
                            "price": 14
                        }
                    ]
                }
            ]
        },
        {
            "_id": "2",
            "nom": "tech",
            "blocks": [
                {
                    "idBlock": "1",
                    "ArtisanOffres": [
                        {
                            "idArtisan": "1",
                            "price": 12
                        }
                    ]
                },
                {
                    "idBlock": "2",
                    "ArtisanOffres": [
                        {
                            "idArtisan": "1",
                            "price": 14
                        },
                        {
                            "idArtisan": "2",
                            "price": 50
                        }
                    ]
                }
            ]
        }
        
    ];
    let result = SubSpecs.map(sub => {
        return sub.blocks.map(block => {
         return block.ArtisanOffres.filter(artisan => artisan.idArtisan === "1");
       });
    });
    let array = result.flat(2);
    let final_sum = {};
    final_sum.idArtisan = "1";
    final_sum.total = array.reduce(function(_this, val) {
          return _this + val.price
      }, 0);
    console.log(final_sum);

说明:使用了 2 个地图(因为 SubSpecs 和块是数组),然后是 idArtisan 的过滤器。 result.flat(2) 是因为 maps 结果创建了一个 3 级数组。最后是一个汇总价格的减速器。

【讨论】:

  • 它返回由 3 个对象组成的数组,如下所示: [{idArtisan: "1", price:12},{idArtisan: "1", price:14},{idArtisan: "1", price :12},{idArtisan: "1", price:14}] !!!它过滤但没有返回总和!有什么问题!!
  • Ciao,我更新了我的答案。现在您有了一个对象 (final_sum),其中包含 idArtisan 和总价。
  • 感谢它的作品,非常感谢您的帮助:)
  • 没问题。祝你有美好的一天:)
  • 祝你有美好的一天
猜你喜欢
  • 1970-01-01
  • 2022-01-12
  • 2020-05-23
  • 1970-01-01
  • 2021-04-14
  • 2020-08-25
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
相关资源
最近更新 更多