如何对对象数组进行分组并转换重复项

发布于 2025-01-12 01:39:07 字数 924 浏览 0 评论 0原文

大家好,我有这个数据结构,我需要对其进行分组,如果有任何重复项可以将这两个对象转换为一行。.

数据结构

var shipTo = [
  { addressLine1: "address 1", name: "Jeff" },
  { addressLine1: "address 2", name: "Taylor" },
  { addressLine1: "address 1", name: "Megan" },
  { addressLine1: "address 3", name: "Madison" },
];

分组函数

function groupArrayOfObjects(list, key) {
  return list.reduce(function (rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
}

我有groupedBy 地址 attrube 通过上面这个函数的地址

const groupedAddress = groupArrayOfObjects(shipTo, "addressLine1");

我的新数据结构是

在此处输入图像描述

我的最终结果希望是这样的:

货件 1 和 1 的送货地址3 是“地址 1”

货件 2 的送货地址是“地址 2”

Hello to all i have this data structure that i need to group and if there are any duplicates to convert those two objects it in one line..

Data Structure

var shipTo = [
  { addressLine1: "address 1", name: "Jeff" },
  { addressLine1: "address 2", name: "Taylor" },
  { addressLine1: "address 1", name: "Megan" },
  { addressLine1: "address 3", name: "Madison" },
];

Grouped function

function groupArrayOfObjects(list, key) {
  return list.reduce(function (rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
}

I have groupedBy address attrube by this function above by address

const groupedAddress = groupArrayOfObjects(shipTo, "addressLine1");

My new data-structure is

enter image description here

My final Result wanna be like this :

Shipping Address for Shipment 1 & 3 is "address 1"

Shipping Address for Shipment 2 is "address 2"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2025-01-19 01:39:07

reduce 可以将当前数组元素的索引作为第三个参数。然后你可以将它传递给你的累加器,并在最终的数组中可以用它做你想做的事情

var shipTo = [
  { addressLine1: "address 1", name: "Jeff" },
  { addressLine1: "address 2", name: "Taylor" },
  { addressLine1: "address 1", name: "Megan" },
  { addressLine1: "address 3", name: "Madison" },
];

function groupArrayOfObjects(list, key) {
  return list.reduce(function (rv, x, idx) {
    (rv[x[key]] = rv[x[key]] || []).push({...x, index:idx+1});
    return rv;
  }, {});
}

const groupedAddress = groupArrayOfObjects(shipTo, "addressLine1");
console.log(groupedAddress)


//for completeness
Object.values(groupedAddress).forEach((address) => {
    let addressName = address[0].addressLine1
  let joinedIndices = address.map(({index}) => index).join(" & ")
  console.log(`Shipping Address for Shipment ${joinedIndices} is "${addressName}"`)
})

reduce can take the index of the current array element as the 3rd parameter. you can then pass it to your accumulator and in the final array can do what you want with it

var shipTo = [
  { addressLine1: "address 1", name: "Jeff" },
  { addressLine1: "address 2", name: "Taylor" },
  { addressLine1: "address 1", name: "Megan" },
  { addressLine1: "address 3", name: "Madison" },
];

function groupArrayOfObjects(list, key) {
  return list.reduce(function (rv, x, idx) {
    (rv[x[key]] = rv[x[key]] || []).push({...x, index:idx+1});
    return rv;
  }, {});
}

const groupedAddress = groupArrayOfObjects(shipTo, "addressLine1");
console.log(groupedAddress)


//for completeness
Object.values(groupedAddress).forEach((address) => {
    let addressName = address[0].addressLine1
  let joinedIndices = address.map(({index}) => index).join(" & ")
  console.log(`Shipping Address for Shipment ${joinedIndices} is "${addressName}"`)
})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文