删除重复的键并在JavaScript数组中合并唯一值

时间:2020-06-01 19:13:38

标签: javascript arrays merge lodash

我有一个数组,其中包含客户ID,交易价值和客户执行的每笔交易的交易ID。

我有9,000个客户执行了20,000个交易。 我想要一个客户ID,每个客户ID的所有价格的数组以及每个客户ID的所有交易ID的数组。

当前看起来像这样:

[Running] cd "c:\AdamWilson\Code\Languages\C++\MightWork\" && gcc pleasework.c -o pleasework && "c:\AdamWilson\Code\Languages\C++\MightWork\"pleasework
C:/AdamWilson/Code/Languages/C++/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x39): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

,我希望它看起来像这样:

var transactionArray =
{
  customerId: '1',
  price: [ 100 ],
  transactionID: ['00a13']
},
{
  customerId: '2',
  price: [ 200 ],
  transactionID: ['00a14']
},
{
  customerId: '1',
  price: [ 700 ],
  transactionID: ['00a15']
},
{
  customerId: '2',
  price: [ 1700 ],
  transactionID: ['00a16']
},

... 19996 more items

2 个答案:

答案 0 :(得分:1)

只需使用reduce并将元素推入数组

var transactionArray = [{
    customerId: '1',
    price: [100],
    transactionID: ['00a13']
  },
  {
    customerId: '2',
    price: [200],
    transactionID: ['00a14']
  },
  {
    customerId: '1',
    price: [700],
    transactionID: ['00a15']
  },
  {
    customerId: '2',
    price: [1700],
    transactionID: ['00a16']
  },
]

var results = Object.values(transactionArray.reduce((custs, { customerId, price, transactionID }) => {
  var customer = custs[customerId]
  if (!customer) {
    custs[customerId] = {
      customerId: customerId,
      price: [...price],
      transactionID: [...transactionID]
    }
  } else {
    customer.price = [...customer.price, ...price]
    customer.transactionID =  [...customer.transactionID, ...transactionID]
  }

  return custs
}, {}))

console.log(results)

答案 1 :(得分:0)

customerId为整数很方便。如果有任何变化,则需要为它们建立索引,然后重新构建对象。

// create a separate array for holding order of customerIds
const customerIds = []

const result = transactionArray
  .reduce((acc, { customerId, price, transactionID }) => {
    const idIndex = customerIds.indexOf(customerId)
    // check if customerId exists in customerIds array
    if (idIndex < 0) {
      // if it doesn't, add it to the customerId's array
      customerIds.push(customerId)
      // then add the matching price and transactionID to the accumulator
      // of this reduce, spreading the contents of the array into 2 new arrays.
      acc.push([[...price], [...transactionID]])
    } else {
      // if the customerId is already accounted for, then
      // add the price and transactionID to that customer's bucket
      // at the same index where the customerId exists inside customerIds
      acc[idIndex][0].push(...price)
      acc[idIndex][1].push(...transactionID)
    }
    return acc
  }, [])
  // finally, convert the arrays back into objects
  .map((value, index) => {
    return ({ 
      customerId: customerIds[index],
      price: value[0],
      transactionID: value[1],
    })
  })


console.log(result)

记录:

[
  {
    customerId: '1',
    price: [ 100, 700 ],
    transactionID: [ '00a13', '00a15' ]
  },
  {
    customerId: '2',
    price: [ 200, 1700 ],
    transactionID: [ '00a14', '00a16' ]
  }
]

如果customerId是不表示整数的字符串,则该字符串仍然有效-例如,如果您的客户数据如下所示:

const transactionArray = [
  {
    customerId: '324asdrea',
    price: [ 100 ],
    transactionID: ['00a13']
  },
  {
    customerId: '4hdffgi2',
    price: [ 200 ],
    transactionID: ['00a14']
  },
  {
    customerId: '324asdrea',
    price: [ 700 ],
    transactionID: ['00a15']
  },
  {
    customerId: '4hdffgi2',
    price: [ 1700 ],
    transactionID: ['00a16']
  }
]

结果为:

[
  {
    customerId: '324asdrea',
    price: [ 100, 700 ],
    transactionID: [ '00a13', '00a15' ]
  },
  {
    customerId: '4hdffgi2',
    price: [ 200, 1700 ],
    transactionID: [ '00a14', '00a16' ]
  }
]