如何从对象的平面数组创建对象的嵌套数组? [英] How to create a nested array of objects from flat array of objects?

查看:66
本文介绍了如何从对象的平面数组创建对象的嵌套数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习,并且需要一些有关如何从JSON平面文件中形成对象的复杂嵌套数组的指导.

I am still learning and need some guidance on how to form a complex nested array of objects from a JSON flat file.

这是当前输入:

[
{"id":"US-AL","state":"Alabama","industry":"All","category":"Cable Related Services","itemid":"12290","item":"Basic Cable Services","answer":"Exempt","explanation":"The sale of these services is not subject to sales tax.","citation":"Ala. Code sec. 40-23-1; Ala. Code sec. 40-23-2"},
{"id":"US-AL","state":"Alabama","industry":"All","category":"Cable Related Services","itemid":"12291","item":"Pay Per View","answer":"Exempt","explanation":"The sale of these services is not subject to sales tax.","citation":"Ala. Code sec. 40-23-1; Ala. Code sec. 40-23-2"},
{"id":"US-AL","state":"Alabama","industry":"Retail","category":"Sales Tax Holidays","itemid":"12524","item":"All Disaster Preparedness Supply","answer":"Exempt","explanation":"Alabama provides for an annual state sales tax holiday for severe weather preparedness items. Counties and municipalities are allowed to provide an exemption from local sales and use taxes from the same items during the same weekend as the state holiday.","citation":"Ala. Admin. Code r. 810-6-3-.66."},
{"id":"US-AL","state":"Alabama","industry":"Retail","category":"Sales Tax Holidays","itemid":"12525","item":"All Energy star qualified products","answer":"N/A","explanation":"Alabama does not provide a sales tax holiday for energy efficient products.","citation":"N/A"}
]

这是我想要的格式:

[
   {
    "id":"US-AL",
    "state":"Alabama", 
    "industries" [ 
        {
           "industry":"All", 
           "categories" [
              {
                 "category":"Cable Related Services",
                  items [
                     {"itemid":"12290","item":"Basic Cable Services","answer":"Exempt","explanation":"The sale of these services is not subject to sales tax.","citation":"Ala. Code sec. 40-23-1; Ala. Code sec. 40-23-2"}, 
                     {"itemid":"12291","item":"Pay Per View","answer":"Exempt","explanation":"The sale of these services is not subject to sales tax.","citation":"Ala. Code sec. 40-23-1; Ala. Code sec. 40-23-2"}
                   ],
                 "category":"Sales Tax Holidays",
                 items [
                     {"itemid":"12524","item":"All Disaster Preparedness Supply","answer":"Exempt","explanation":"Alabama provides for an annual state sales tax holiday for severe weather preparedness items. Counties and municipalities are allowed to provide an exemption from local sales and use taxes from the same items during the same weekend as the state holiday.","citation":"Ala. Admin. Code r. 810-6-3-.66."}
                  ]
              }
           ],
           "industry":"Sales" ...
    "id":"US-AR",
    "state":"Arizona" ... 
]

我尝试使用.map,.reduce,.filter ...

I've tried using .map, .reduce, .filter ...

使用此示例,我能够格式化一个级别,但是不确定这是否是正确的方法,或者不确定是否有更简单的方法来完成此操作.

Using this example, I was able to get one level formatted, but not sure if this is the right method or if there is an easier way to accomplish this.

var grouped = utm.reduce((r, o) => {
  r[o.industry] = r[o.industry] || [];
  r[o.industry].push(o);
  return r;
}, {});
var rs = Object.keys(grouped).map(industry => ({ industry, categories: grouped[industry] }));

推荐答案

我从以下SO帖子中找到了一个很好的答案:

I found a great answer to this from this SO post: Convert flat array of objects into nested array of objects

我尝试了这个,它确实做了我想做的事情.

I tried this and it does exactly what I was trying to do.

// Groups a flat array into a tree. 
// "data" is the flat array.
// "keys" is an array of properties to group on.
function groupBy(data, keys) { 

  if (keys.length == 0) return data;

  // The current key to perform the grouping on:
  var key = keys[0];

  // Loop through the data and construct buckets for
  // all of the unique keys:
  var groups = {};
  for (var i = 0; i < data.length; i++)
  {
      var row = data[i];
      var groupValue = row[key];

      if (groups[groupValue] == undefined)
      {
          groups[groupValue] = new Array();
      }

      groups[groupValue].push(row);
  }

  // Remove the first element from the groups array:
  keys.reverse();
  keys.pop()
  keys.reverse();

  // If there are no more keys left, we're done:
  if (keys.length == 0) return groups;

  // Otherwise, handle further groupings:
  for (var group in groups)
  {
      groups[group] = groupBy(groups[group], keys.slice());
  }

  return groups;
}

然后我在此处指定要分组的字段:

Then I specify what fields I want to be grouped here:

var UtmDataByState = groupBy(utm, ["id","industry","category"]);

做得很棒!谢谢史蒂夫:)

Worked great! Thanks Steve :)

这篇关于如何从对象的平面数组创建对象的嵌套数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆