【问题标题】:Need help using lodash to reform an array with new structure需要帮助使用 lodash 用新结构改造数组
【发布时间】:2015-03-14 23:54:00
【问题描述】:

我有这个从 API 返回的示例数据,我试图将其重组为具有分组对象子数组的数组。 API 的原始数据如下所示:

[
    {
        "InsightId": 314,
        "Classification": "Advantage",
        "AttributeId": 14958,
        "InsightAttribute": "Implementation speed",
        "AttributeType": "Product Criterion"
    },
    {
        "InsightId": 314,
        "Classification": "Advantage",
        "AttributeId": 14976,
        "InsightAttribute": "Understanding your needs",
        "AttributeType": "Sales Criterion"
    },
    {
        "InsightId": 315,
        "Classification": "Disadvantage",
        "AttributeId": 17691,
        "InsightAttribute": "Poor alignment with needs",
        "AttributeType": "Product Criterion"
    }
]

我想按 InsightId 进行分组,并从三个属性中创建一个对象:AttributeId、InsightAttribute、AttributeType。

并让最终数组采用以下形式:

[
    {
        "InsightId": 314,
        "Classification": "Advantage",
        "Attributes" : [
            {
                "AttributeId": 14958,
                "InsightAttribute": "Implementation speed",
                "AttributeType": "Product Criterion"
            },

            {
                AttributeId": 14976,
                "InsightAttribute": "Understanding your needs",
                "AttributeType": "Sales Criterion"
            }
        ]
    },
    {
        "InsightId": 315,
        "Classification": "Disadvantage",
        "Attributes" : [
            {
                "AttributeId": 17691,
                "InsightAttribute": "Poor alignment with needs",
                "AttributeType": "Product Criterion"
            }
        ]   
    }
]

我是 Lodash 的新手,我已经花了好几个小时在几个没有成功的兔子洞里摸索。我意识到是时候求助于专家了。关于如何获得我上面显示的最终结果的任何建议?

感谢您的帮助!

【问题讨论】:

  • 没有理由为此使用 lodash,您可以使用纯 JavaScript 轻松完成。

标签: javascript arrays grouping lodash


【解决方案1】:

您可以使用 reduce() 循环并建立一个新的响应:

  _.values( _.reduce([{
    "InsightId": 314,
    "Classification": "Advantage",
    "AttributeId": 14958,
    "InsightAttribute": "Implementation speed",
    "AttributeType": "Product Criterion"
  }, {
    "InsightId": 314,
    "Classification": "Advantage",
    "AttributeId": 14976,
    "InsightAttribute": "Understanding your needs",
    "AttributeType": "Sales Criterion"
  }, {
    "InsightId": 315,
    "Classification": "Disadvantage",
    "AttributeId": 17691,
    "InsightAttribute": "Poor alignment with needs",
    "AttributeType": "Product Criterion"
  }], function(a, b) {

    // create a new array for common items under InsightId:
    var temp=a[b.InsightId] = a[b.InsightId] || {
        InsightId: b.InsightId,
        Classification: b.Classification,
        Attributes: []
    };

    // push current attribs into collection under InsightId:
    temp.Attributes.push({
        AttributeId: b.AttributeId,
        InsightAttribute: b.InsightAttribute,
        AttributeType: b.AttributeType
    });
    return a;
  }, {}));

回馈:

[
    {
        "InsightId": 314,
        "Classification": "Advantage",
        "Attributes": [
            {
                "AttributeId": 14958,
                "InsightAttribute": "Implementation speed",
                "AttributeType": "Product Criterion"
            },
            {
                "AttributeId": 14976,
                "InsightAttribute": "Understanding your needs",
                "AttributeType": "Sales Criterion"
            }
        ]
    },
    {
        "InsightId": 315,
        "Classification": "Disadvantage",
        "Attributes": [
            {
                "AttributeId": 17691,
                "InsightAttribute": "Poor alignment with needs",
                "AttributeType": "Product Criterion"
            }
        ]
    }
]

可能有一种稍微简单的方法来收集属性,但只有 3 个键,硬编码并不太麻烦。

【讨论】:

  • 这是一个很好的解决方案,效果也很好。感谢您的帮助,因为它也帮助我学习了新的编程概念。
【解决方案2】:

这里 lodash 最有用的部分是pick,它允许您按名称选择性地切出对象的某些属性。

基本上,而不是这个:

obj1 = {
  name: obj2.name
  age: obj2.age
  size: obj2.size
}

你可以这样做

obj1 = _.pick(obj1, 'name', 'age', 'size')

您可以迭代您的数据,在一个对象中构建您所需的结构,以便通过InsightId 轻松查找,然后使用_.values 将其转换为您需要的平面数组。

    data = [
        {
            "InsightId": 314,
            "Classification": "Advantage",
            "AttributeId": 14958,
            "InsightAttribute": "Implementation speed",
            "AttributeType": "Product Criterion"
        },
        {
            "InsightId": 314,
            "Classification": "Advantage",
            "AttributeId": 14976,
            "InsightAttribute": "Understanding your needs",
            "AttributeType": "Sales Criterion"
        },
        {
            "InsightId": 315,
            "Classification": "Disadvantage",
            "AttributeId": 17691,
            "InsightAttribute": "Poor alignment with needs",
            "AttributeType": "Product Criterion"
        }
    ]
    
    hash = {}
    
    data.forEach(function (r) {
      if (!hash[r.InsightId])
        hash[r.InsightId] = _.merge(
          _.pick(r, 'InsightId', 'Classification'), {Attributes: []}
        );

      hash[r.InsightId].Attributes.push(_.pick(r, "AttributeId", "InsightAttribute", "AttributeType"))
    });


    output = _.values(hash);
    

    console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.js"></script>

【讨论】:

  • 这就是我想要的。工作得很漂亮。谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多