微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从平面对象数组创建嵌套对象数组?

如何解决如何从平面对象数组创建嵌套对象数组?

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

这是当前输入:

[
{"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","itemid":"12291","item":"Pay Per View","industry":"Retail","category":"Sales Tax Holidays","itemid":"12524","item":"All disaster Preparedness Supply","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."},"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","industries" [ 
        {
           "industry":"All","categories" [
              {
                 "category":"Cable Related Services",items [
                     {"itemid":"12290",{"itemid":"12291","citation":"Ala. Code sec. 40-23-1; Ala. Code sec. 40-23-2"}
                   ],items [
                     {"itemid":"12524","citation":"Ala. Admin. Code r. 810-6-3-.66."}
                  ]
              }
           ],"industry":"Sales" ...
    "id":"US-AR","state":"Arizona" ... 
]

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

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

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帖子中找到了一个很好的答案:Convert flat array of objects into nested array of objects

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

// 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;
}

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

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

工作出色!谢谢史蒂夫:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。