javascript - 将平面数组减少为 2 层嵌套 json 对象数组

标签 javascript json nested reduce

我有以下平面数组:

{ "State": "New York", "Name": "Jane", "Product": "Apple" },
{ "State": "New York", "Name": "Jill", "Product": "Banana"},
{ "State": "California", "Name": "Jill", "Product": "Apple" },
{ "State": "California", "Name": "Jill", "Product": "Banana"}

是否可以创建 2 级嵌套数组(即“名称”>“嵌套状态数组”>“嵌套产品数组”)?它看起来像下面这样:

{
  "Name": "Jill",
  "States": [
   {
   "State": "California",
   "Products": [
      {
    "Product": "Apple"
      },
      {
          "Product": "Banana"
      }
    ]
   },
   {
   "State": "New York",
   "Products": [
      {
          "Product": "Banana"
      }
    ]
   }
  ]
 },
 {
  "Name": "Jane",
  "States": [
   {
   "State": "New York",
   "Products": [
     {
      "Product": "Apple"
     }
    ]
   }
  ]
 }

我已经能够嵌套一层(状态)。您将如何嵌套第二层?

这是一个堆栈 Blitz :https://stackblitz.com/edit/angular-lu6zj2

this.grouped_data = this.data.reduce((data, item) => {
  data[item.Name] = data[item.Name] || { Name: item.Name, States: []}
  data[item.Name].States.push(item) 
  return data;
}, {})

最佳答案

let data = [
  { "State": "New York", "Name": "Jane", "Product": "Apple" },
  { "State": "New York", "Name": "Jill", "Product": "Banana"},
  { "State": "California", "Name": "Jill", "Product": "Apple" },
  { "State": "California", "Name": "Jill", "Product": "Banana"}
];

let grouped = data.reduce((p, n) => {
  // Create the Lady
  if (!p[n.Name]) p[n.Name] = { States: [] };
  // Check if the state exists, if not create it, then push product into it
  if (!p[n.Name].States.some(state => state.State === n.State)) {
    p[n.Name].States.push({ State: n.State, Products: [n.Product] });
  } else {
    !p[n.Name].States.find(state => state.State === n.State).Products.push(n.Product);
  }
  return p;
}, {});

console.log(grouped);

之后,您还可以根据需要删除重复的产品。我会让你处理的!

编辑我不尊重你的模型,我真是个傻瓜......这就是:

let data = [
  { "State": "New York", "Name": "Jane", "Product": "Apple" },
  { "State": "New York", "Name": "Jill", "Product": "Banana"},
  { "State": "California", "Name": "Jill", "Product": "Apple" },
  { "State": "California", "Name": "Jill", "Product": "Banana"}
];

let grouped = data.reduce((p, n) => {
  // Create the Lady
  if (!p.some(lady => lady.Name === n.Name)) p.push({ Name: n.Name, States: [] });
  let lady = p.find(lady => lady.Name === n.Name);
  // Check if the state exists, if not create it, then push product into it
  if (!lady.States.some(state => state.State === n.State)) {
    lady.States.push({ State: n.State, Products: [n.Product] });
  } else {
    lady.States.find(state => state.State === n.State).Products.push(n.Product);
  }
  return p;
}, []);

console.log(grouped);

关于javascript - 将平面数组减少为 2 层嵌套 json 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56446075/

相关文章:

javascript - 真的有必要使用react-bootstrap提供的组件吗?

javascript - 获取用户 ID 并将其保存到各个行会

javascript - 遍历 JSON 数组中的对象

java - 获取对象键并将其用作 Jackson 中的属性

java - 无法初始化嵌套 Fragment,因为它在旋转上是抽象的

javascript - JSP 页面上 jQuery 验证插件的多个实例

javascript - iframe 完成加载后加载指示器保留在 Firefox 中

jquery - 如何重定向到正确的 Controller 操作方法

javascript - 使用 Javascript 通过嵌套函数返回数组

java - Java 中的模块化 : top level vs. 嵌套类