使用JavaScript在交易对象数组中查找重复项,并在数组对象数组中查找合并的重复项

时间:2018-11-26 12:48:28

标签: javascript arrays duplicates javascript-objects

我有一个交易对象数组,在这里我需要根据属性查找重复项(如果对象的所有值都相同(除了ID和TIME除外,则时间差应在1分钟之内),则该对象为重复项)。 我需要将相同的重复事务合并为一个数组对象。

下面是交易的输入。

  

我尝试使用Reduce函数,但无法获得预期的输出。

var newArray = transactions.reduce(function(acc, curr) {
      //finding Index in the array where the NamaCategory matched
      var findIfduplicateExist = acc.findIndex(function(item) {
        let accepoch = new Date(item.time).valueOf();
        let currepoch= new Date(curr.time).valueof();
        if(item.sourceAccount === curr.sourceAccount &&
        item.targetAccount===curr.targetAccount &&
        item.amount===curr.amount&&
        accepoch<currepoch+(1*60*1000))
          let obj={
           'id':curr.id,
            'sourceAccount':curr.sourceAccount,
            'targetAccount':curr.targetAccount,
            'time':curr.time,
            'category':curr.category,
            'amount':curr.amount 
          }
      })
      // if in the new array no such object exist, create a new object 
      if (findIfNameExist === -1) {
        acc.push(obj)
      } else {
        // if attributes matches , then push the value 
        acc[findIfNameExist].value.push(curr)
      }
  return acc;

}, []);

输入交易:

[
  {
    id: 3,
    sourceAccount: 'A',
    targetAccount: 'B',
    amount: 100,
    category: 'eating_out',
    time: '2018-03-02T10:34:30.000Z'
  },
  {
    id: 1,
    sourceAccount: 'A',
    targetAccount: 'B',
    amount: 100,
    category: 'eating_out',
    time: '2018-03-02T10:33:00.000Z'
  },
  {
    id: 6,
    sourceAccount: 'A',
    targetAccount: 'C',
    amount: 250,
    category: 'other',
    time: '2018-03-02T10:33:05.000Z'
  },
  {
    id: 4,
    sourceAccount: 'A',
    targetAccount: 'B',
    amount: 100,
    category: 'eating_out',
    time: '2018-03-02T10:36:00.000Z'
  },
  {
    id: 2,
    sourceAccount: 'A',
    targetAccount: 'B',
    amount: 100,
    category: 'eating_out',
    time: '2018-03-02T10:33:50.000Z'
  },
  {
    id: 5,
    sourceAccount: 'A',
    targetAccount: 'C',
    amount: 250,
    category: 'other',
    time: '2018-03-02T10:33:00.000Z'
  }
];

预期输出如下:

[   
  [
    {
      id: 1,
      sourceAccount: "A",
      targetAccount: "B",
      amount: 100,
      category: "eating_out",
      time: "2018-03-02T10:33:00.000Z"
    },
    {
      id: 2,
      sourceAccount: "A",
      targetAccount: "B",
      amount: 100,
      category: "eating_out",
      time: "2018-03-02T10:33:50.000Z"
    },
    {
      id: 3,
      sourceAccount: "A",
      targetAccount: "B",
      amount: 100,
      category: "eating_out",
      time: "2018-03-02T10:34:30.000Z"
    }  
  ], 
  [
    {
      id: 5,
      sourceAccount: "A",
      targetAccount: "C",
      amount: 250,
      category: "other",
      time: "2018-03-02T10:33:00.000Z"
    },
    {
      id: 6,
      sourceAccount: "A",
      targetAccount: "C",
      amount: 250,
      category: "other",
      time: "2018-03-02T10:33:05.000Z"
    }   
  ] 
]

4 个答案:

答案 0 :(得分:1)

您可以进行多列排序,然后在每组中查找重复项。

const SECONDS = 60;
const MILLISECONDS = 1000;

const getTimeDifference = (t1, t2) => {
  return new Date(t1) - new Date(t2);
};

const multiLevelSort = (transactions = [], colsToSort = []) => {
  return transactions.sort((a, b) => {
    return colsToSort.reduce((acc, col) => {
      if (acc !== 0 || a[col] == b[col]) {
        return acc;
      }

      const c1 = a[col], c2 = b[col];
      if (col === "time") {
        return getTimeDifference(c1, c2) > 0 ? 1 : -1;
      } else {
        return c1 > c2 ? 1 : -1;
      }
    }, 0);
  });
};

const isUniqueTransaction = (prev, curr, matchKeys = []) => {
  if (!prev || !curr) {
    return true;
  }
  return matchKeys.reduce((acc, key) => {
    /* Current key is time then difference should be more than equal
     * 1 min for transaction to be unique.
     */
    if (key === "time") {
      return (
        acc ||
        getTimeDifference(curr[key], prev[key]) >= 1 * SECONDS * MILLISECONDS
      );
    }

    return acc || prev[key] !== curr[key];
  }, false);
};

function findDuplicateTransactions(transactions = []) {
  const matchingKeys = [
    "sourceAccount",
    "targetAccount",
    "amount",
    "category",
    "time"
  ];
  const sortedTransactions = multiLevelSort(transactions, matchingKeys);
  let duplicates = [];
  let group = [];
  sortedTransactions.forEach((curr, idx, transactions) => {
    // Previous Transaction find check if current trasaction is unique.
    const prev = group && group[group.length - 1];
    const isUnique = isUniqueTransaction(prev, curr, matchingKeys);
    if (isUnique) {
      if (group.length > 1) {
        duplicates.push(group);
      }
      group = [];
    }
    group.push(curr);
  });

  // Push last group if it has more than 1 transaction
  if (group.length > 1) {
    duplicates.push(group);
  }

  // Sort duplicate trasaction groups based on first transaction in group
  return duplicates.sort((a, b) => {
    return getTimeDifference(a[0].time, b[0].time);
  });
}

答案 1 :(得分:0)

当您第一次获得按ID排序的交易副本时,它将更加容易(并且效率更高)。我认为id是一个递增的数字,因此以后的事务始终具有更大的数字。这样,您只需要比较时间戳和累加器中的最后一个时间戳即可:

// Example data
const transactions = [ { id: 3, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:34:30.000Z' }, { id: 1, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:33:00.000Z' }, { id: 6, sourceAccount: 'A', targetAccount: 'C', amount: 250, category: 'other', time: '2018-03-02T10:33:05.000Z' }, { id: 4, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:36:00.000Z' }, { id: 2, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:33:50.000Z' }, { id: 5, sourceAccount: 'A', targetAccount: 'C', amount: 250, category: 'other', time: '2018-03-02T10:33:00.000Z' } ];

const newArray = [...transactions].sort((a,b) => a.id - b.id).reduce( (acc, curr) => {
    let group = acc[acc.length-1], 
        prev = group && group[group.length-1];
    if (!prev || prev.sourceAccount !== curr.sourceAccount ||
                 prev.targetAccount !== curr.targetAccount ||
                 prev.amount !== curr.amount ||
                 Date.parse(prev.time) + (1*60*1000) < Date.parse(curr.time)) {
        // different keys or larger time difference: create new group
        acc.push(group = []);
    }
    group.push(curr);
    return acc;
}, []);

console.log(newArray);

答案 2 :(得分:0)

您也可以像下面一样使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <ul class="image"> </ul>Array.sort来实现这一目标

我最初通过连接属性值(不包括Array.forEachid)并按增加的时间戳对数组进行排序

time

答案 3 :(得分:0)

这可以通过一个Array.sortArray.reduceObject.values来简明扼要:

const data = [{ id: 3, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:34:30.000Z' }, { id: 1, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:33:00.000Z' }, { id: 6, sourceAccount: 'A', targetAccount: 'C', amount: 250, category: 'other', time: '2018-03-02T10:33:05.000Z' }, { id: 4, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:36:00.000Z' }, { id: 2, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:33:50.000Z' }, { id: 5, sourceAccount: 'A', targetAccount: 'C', amount: 250, category: 'other', time: '2018-03-02T10:33:00.000Z' }] 

const sort = arr => arr.sort((a,b) =>`${a.id}${a.time}`.localeCompare(`${b.id}${b.time}`))
const getTime = obj => new Date(obj.time).getTime()
const isDub = (arr, obj) => arr.length ? Math.abs(getTime(arr[arr.length-1]) - getTime(obj))/1000 > 60 : false

const result = Object.values(sort(data).reduce((r, c) => {
  let key = [c.sourceAccount, c.targetAccount].join('-')
  r[key] = isDub(r[key] || [], c) ? r[key] : [...r[key] || [], c]
  return r
}, {}))

console.log(result)

您确实需要对数组进行预排序,以便根据分钟内的要求比较重复项时只处理最后一个条目。