繁体   English   中英

我将如何将对象数组动态聚合成组

[英]how would i aggregate array of objects into groups, dynamically

我有一些动态数据。 假设 id 将始终存在,并且其他 key:values 将更改。

我试图围绕一种抽象的方法进行思考,但我认为我对可用的工具了解不够。

我正在寻找一种动态聚合的方法。

例如,如果我有这个数据:

const dataum = [{
  "id": 1,
  "q1": "yes",
  "q2": 4,
  "q3": null
}, {
  "id": 2,
  "q1": null,
  "q2": 14,
  "q3": "medium"
}, {
  "id": 3,
  "q1": "no",
  "q2": 83,
  "q3": "high"
}, {
  "id": 27,
  "q1": "yes",
  "q2": 62,
  "q3": null
}]

并试图创建这样的东西:

const aggData = [{
  "q1": {
    "yes": 2,
    "empty": 1,
    "no": 1
  }
}, {
  "q2": {
    "0to60": 2,
    "60to70": 1,
    "70to100": 1,
    "empty": 0
  }
} {
  "q3": {
    "medium": 1,
    "empty": 2,
    "high": 1
  }
}]

我没有尝试太多。 因为我被困在最外面的过程/功能上。 我想过 for() .keys 和 .values,我看过 array.reduce array.reduce() ...但我仍然不明白如何能够动态地说: newObject.key.someAnswer += 1 where the key是动态的,答案是动态的(或为空)。

我不会太担心范围......我想只要有一点帮助,我就可以分叉这个过程。

我可以以任何格式为这个过程提供更多数据,例如:

const qProperties = [{
  "q1": {
    "type": "YN",
    "values": ["yes", "no"]
  }
}, {
  "q3": {
    "type": "LMH",
    "values": ["low", "medium", "high"]
  }
}]

如果它有助于减少代码占用空间,请使其更抽象。

尽管您的输入数据与您预期的输出数据并不真正匹配(这可能是您获得否决票的原因),但我认为您想要这样的东西:

const dataum = [
  { id: 1, q1: "yes", q2: 4, q3: null },
  { id: 2, q1: null, q2: 14, q3: "medium" },
  { id: 3, q1: "no", q2: 83, q3: "high" },
  { id: 27, q1: "yes", q2: 62, q3: null },
];
const intermediate1 = dataum.reduce((acc, curr) => {
  Object.entries(curr).forEach(([k,v]) => {
    if (k !== "id") {
      if (!acc[k]) {
        acc[k] = [];
      }
      acc[k].push(v);
    }
  });
  return acc;
}, {});

console.log(intermediate1);
// {
//   q1: [ 'yes', null, 'no', 'yes' ],
//   q2: [ 4, 14, 83, 62 ],
//   q3: [ null, 'medium', 'high', null ]
// }

const final = Object.entries(intermediate1).reduce((acc, [k, v]) => {
  const accumulated = v.reduce((inner_acc, inner_val) => {
    if (inner_val === null) {
      inner_val = "empty";
    }
    if (!inner_acc[inner_val]) {
      inner_acc[inner_val] = 0;
    }
    inner_acc[inner_val] += 1;
    return inner_acc;
  }, {});    
  acc.push({
    [k]: accumulated,
  });
  return acc;
}, []);

console.log(final);      
// [
//   { q1: { yes: 2, empty: 1, no: 1 } },
//   { q2: { '4': 1, '14': 1, '62': 1, '83': 1 } },
//   { q3: { empty: 2, medium: 1, high: 1 } }
// ]

你可能想看看 lodash,也许他们已经有类似的东西了。

如果您不关心实现,您可以使用lodash.groupby

https://www.npmjs.com/package/lodash.groupby

const groupBy = require('lodash.groupby');

const keys = ["q1", "q2", "q2"];
const dataum = [
  { id: 1, q1: "yes", q2: 4, q3: null },
  { id: 2, q1: null, q2: 14, q3: "medium" },
  { id: 3, q1: "no", q2: 83, q3: "high" },
  { id: 27, q1: "yes", q2: 62, q3: null }
];

const grouped = [];

function countKeys(object) {
  const counts = [];
  const keys = Object.keys(object);

  keys.forEach((key) => {
    const count = Object.values(object[key]).length;
    counts.push({ [key]: count });
  });

  return counts;
}

keys.forEach((key) => {
  const groups = _.groupBy(dataum, key);
  const counts = countKeys(groups);
  const groupedRow = { [key]: counts };
  grouped.push(groupedRow);
});

console.log(grouped);

// const output = [
//   { q1: [{ yes: 2 }, { null: 1 }, { no: 1 }] },
//   { q2: [{ "4": 1 }, { "14": 1 }, { "62": 1 }, { "83": 1 }] },
//   { q2: [{ "4": 1 }, { "14": 1 }, { "62": 1 }, { "83": 1 }] }
// ];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM