Javascript 按多个键筛选对象数组

Javascript 按多个键筛选对象数组,javascript,arrays,Javascript,Arrays,我有一个对象数组,带有javascript的重复键: var transactions = [ { id: 3, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:34:30.000Z'

我有一个对象数组,带有javascript的重复键:

var 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'
          }
        ];
我需要根据“重复键”来过滤并减少数组 sourceAccount:'A',targetAccount:'C'和amount:250,我需要创建一个包含重复对象的新对象数组。希望与纯JS

编辑

期望值

var expected = [
          {
            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: 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'
          }
        ];

就我的所有意图而言,y需要指定密钥的值,但它可以是动态的,因此我们的想法是将数组映射到一个新数组中,其中sourceAccount、targetAccount、category和amount是相同的值,而不知道以前的值。

您可以使用filter方法

var newArray = transactions.filter(f => f.sourceAccount =='A' && f.targetAccount == 'C' && f.amount == 250)

它将返回所有符合条件的对象的数组

将匿名方法传递给javascript的过滤器,该过滤器返回true或false方法允许您一次性减少对象:

var done = new Set()
transactions.filter(obj => {
  if (!done.has(obj.id)) {
    done.add(obj.id)
    return true
  } else {
    return false
  }
})

到目前为止,您尝试了什么?我在这里没有看到任何重复的键。@KoshVery尝试了reduce,但我需要一个键作为引用,所以我不知道从哪里开始。@OmarFaruque sourceAccount:'a',数组中几乎所有位置都是重复的。如果我理解正确,您希望在
sourceAccount
targetAccount
相同的情况下合并数据。请在问题本身中添加此输入的预期输出。目前还不清楚。如果我为键提供值,它会起作用,但我需要过滤掉引用,我的意思是数组应该在不传递key=value的情况下进行过滤,知道吗?但你不能这样做-你给我们指定了
键值
对,所以它不会自动发生。@JackBashford,我的意思是,数组可以通过数字方式生成,因此值可以更改,我不能在filter函数中设置值,因为数组应该通过其自身的属性进行过滤。您必须传入一些内容。要为其查找重复项的已知属性或属性名称的值。您不能只在任何属性组合上查找重复项。要检查的排列可能是大量的。或者我完全误解了这个问题,我可以传递属性键名,但不能传递值,在您的示例中,您传递的是f.sourceAccount=='A',其思想是获取与f.sourceAccount==N匹配的所有对象,其中N可以是任何值