使用 lodash 将具有相同键的对象的值合并到一个数组中

Merge values of objects with same keys into an array using lodash

我有一个对象数组,如下所示:

const childrens = [
    { orderName: "#1004" },
    { orderName: "#1006" },
    { orderName: "#1007" },
    { orderName: "#1005" },
    { deliveryDate: "25-25-25" },
    { signature: "xq" },
  ];

我希望将其转换为对象并具有与这样的值数组相同键的值

{
  orderName: [ '#1004', '#1006', '#1007', '#1005' ],
  deliveryDate: [ '25-25-25' ],
  signature: [ 'xq' ]
}

我现在这样做的方法是使用像这样的 reduce 函数

_.reduce(
    childrens,
    (acc, cur) => {
      const pairs = _.chain(cur).toPairs().flatten().value();

      if (acc[pairs[0]] === undefined) {
        acc[pairs[0]] = [pairs[1]];
      } else {
        acc[pairs[0]].push(pairs[1]);
      }

      return acc;
    },
    {},
  );

我想知道是否有使用内置 lodash 函数的更简洁的方法?

如果你不使用lodash,你可以像下面那样做。

const children = [
  { orderName: "#1004" },
  { orderName: "#1006" },
  { orderName: "#1007" },
  { orderName: "#1005" },
  { deliveryDate: "25-25-25" },
  { signature: "xq" },
];

const output = children.reduce((a, b) => {
  const key = Object.keys(b)[0];
  if (!a[key]) {
    a[key] = [b[key]];
  } else {
    a[key].push(b[key]);
  }
  return a;
}, {});

console.log(output);

您可以将 _.mergeWith() 与数组展开一起使用。合并 2 个值时,检查第一个 (a) 是否仍然是 undefined(空对象),如果是 return 包裹在数组中的第二个项目,如果存在,则使用数组传播以连接它们:

const children = [{"orderName":"#1004"},{"orderName":"#1006"},{"orderName":"#1007"},{"orderName":"#1005"},{"deliveryDate":"25-25-25"},{"signature":"xq"}];

const result = _.mergeWith({}, ...children, (a, b) => 
  _.isUndefined(a) ? [b] : [...a, b]
);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

像这样的东西可以在现代 JS 平台上运行(额外的好处,你不需要 lodash 来使用 reduce)。本质上:

  • 遍历 childrens
  • 中的每个项目
  • 获取该对象的键
  • 循环键
    • 如果结果没有该键,则将其设置为空数组
    • 将键的值推送到结果中的相关数组
const desiredOutput = childrens.reduce(
  (acc, current) => {
    const keys = Object.keys(current);
    keys.forEach(key => {
      if (!acc[key]) {
        acc[key] = [];
      }
      acc[key].push(current[key]);
    });
    return acc;
  },
  {}
);

您可以使用 reduce and Object.entries

使用 vanilla JS 简单地获得结果
(acc[prop] = acc[prop] ?? []).push(value);

const childrens = [
  { orderName: "#1004" },
  { orderName: "#1006" },
  { orderName: "#1007" },
  { orderName: "#1005" },
  { deliveryDate: "25-25-25" },
  { signature: "xq" },
];

const result = childrens.reduce((acc, curr) => {
  const [[prop, value]] = Object.entries(curr);
  (acc[prop] = acc[prop] ?? []).push(value);
  return acc;
}, {});

console.log(result);