Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何从对象的平面阵列创建对象的嵌套阵列?_Arrays_Json_Object_Nested - Fatal编程技术网

Arrays 如何从对象的平面阵列创建对象的嵌套阵列?

Arrays 如何从对象的平面阵列创建对象的嵌套阵列?,arrays,json,object,nested,Arrays,Json,Object,Nested,我仍在学习,需要一些关于如何从JSON平面文件形成复杂的嵌套对象数组的指导 以下是当前输入: [ {"id":"US-AL","state":"Alabama","industry":"All","category":"Cable Related Services","itemid":"12290"

我仍在学习,需要一些关于如何从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","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

使用这个例子,我能够格式化一个级别,但不确定这是否是正确的方法,或者是否有更简单的方法来实现这一点

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] }));

我在这篇文章中找到了一个很好的答案:

我试过这个,它正是我想做的

// 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"]);