【问题标题】:How to filter array by the value of objects' field of nested array?如何通过嵌套数组的对象字段的值过滤数组?
【发布时间】:2019-01-10 08:49:28
【问题描述】:

我有一个对象数组。每个对象都包含另一个数组。 如何通过嵌套aParticipants 数组的sFullName 的值过滤aParticipants 数组?

const filterString = '';
const aParticipants = [{
    'iRoleId': 1,
    'aParticipants': [{
      'iId': 1,
      'sFullName': 'Jimmy'
    }]
  },
  {
    'iRoleId': 2,
    'aParticipants': [{
        'iId': 2,
        'sFullName': 'Tomas'
      },
      {
        'iId': 3,
        'sFullName': 'Stanley'
      }
    ]
  }
]

我已经尝试过这种方法:

const aFilteredParticipants = [...aParticipants].filter(employeeBlock => {
employeeBlock.aParticipants = employeeBlock.aParticipants.filter( item => 
{
return item.sFullName.toLowerCase().includes(filterString.toLowerCase());
});
return employeesBlock.aParticipants.length;
}

这里我根据过滤嵌套数组后剩下的长度过滤父数组。我通过检查来过滤嵌套数组,如果字段sFullName 包含filterString

第一次过滤后输出正确。但是在第一次过滤嵌套数组aParticipants 在父数组aParticipants 的对象中之后,将它们的值更改为[] 空数组。 虽然我正在使用扩展运算符复制初始数组。

【问题讨论】:

  • 是否要过滤数组而不影响原始数组?
  • @xwlee 是的,这就是重点

标签: javascript arrays


【解决方案1】:

const aFilteredParticipants = [...aParticipants].filter(employeeBlock

您不需要[...aParticipants] aParticipants 已经是一个数组,所以它不会有任何区别。

你可以使用下面的

const filterString = 'Jimmy';
const aParticipants = [{
    'iRoleId': 1,
    'aParticipants': [{
      'iId': 1,
      'sFullName': 'Jimmy'
    }]
  },
  {
    'iRoleId': 2,
    'aParticipants': [{
        'iId': 2,
        'sFullName': 'Tomas'
      },
      {
        'iId': 3,
        'sFullName': 'Stanley'
      }
    ]
  }
]

filteredParticipants = aParticipants.filter( x => {
  var isValid = false;

  [].concat(x.aParticipants).some(z => {
    if (z.sFullName.toLowerCase().indexOf(filterString.toLowerCase()) !== -1) {
      isValid = true;
    }

    return isValid;
  })

  return isValid;
})
console.log(filteredParticipants)

【讨论】:

    【解决方案2】:

    const filterString = '';
    
    const aParticipants = [{
        'iRoleId': 1,
        'aParticipants': [{
          'iId': 1,
          'sFullName': 'Jimmy'
        }]
      },
      {
        'iRoleId': 2,
        'aParticipants': [{
            'iId': 2,
            'sFullName': 'Tomas'
          },
          {
            'iId': 3,
            'sFullName': 'Stanley'
          }
        ]
      }
    ]
    
    // here we are checking that a sFullName contains our filter
    const fullNameContains = filter => participant =>
      participant.sFullName.toLowerCase().includes(filter.toLowerCase())
      
    // here we verify that at least one of the aParticipants sub array contains the filter
    const hasFullNames = filter => employeeBlock =>
      employeeBlock.aParticipants.some(fullNameContains(filterString))
    
    // here we filter participants
    const aFilteredParticipants = aParticipants.filter(hasFullNames(filterString))
    
    console.log('aFilteredParticipants', aFilteredParticipants);

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 2021-10-23
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 2021-07-08
      • 2016-11-17
      • 2020-03-08
      • 1970-01-01
      相关资源
      最近更新 更多