[英]How to filter an object that has nested arrays by the second nested array?
I have an object with a structure like below. 我有一个结构如下的对象。
Object{
Array [{
property1: "",
property2: "",
innerArray: [{
arrayProperty1
arrayProperty2
}]
}]
}
What is the best by way to filter by innerArray.Property2? 通过innerArray.Property2进行过滤的最佳方式是什么? I have a list of filters I need to apply.
我有一个需要应用的过滤器列表。 The code below is how I'm currently attempting to apply the filters, however the list is not being altered at all.
下面的代码是我当前尝试应用过滤器的方式,但是列表根本没有被更改。
if(filterList)
{
// if this length is 0, don't include inventory in the filter
let filterOnInventoryId = filterList.filter((item: any) => {
return (item && item.Type === 'InventoryId');
});
let shouldFilterInventoryId = filterOnInventoryId && filterOnInventoryId.length > 0;
let shouldFilterAppointmentType = filterOnAppointmentType && filterOnAppointmentType.length > 0;
// find any inventoryIds that are part of the filter, else return an empty list
let filteredListInventoryId = shouldFilterInventoryId ? filterOnInventoryId.filter((item: any) =>
{
var x = this.selectedDateAndAppointmentList.filter((dateModelAndAppointment: any) =>
{
return dateModelAndAppointment.appointmentList.filter((appointment: any) =>{
item.InventoryTypeId == appointment.Inventory.InventoryTypeId;
})
})
return x;
}) : [];
}
Nesting array filtering such as: 嵌套数组过滤,例如:
selectedDateAndAppointmentList.filter(dateModelAndAppointment =>
dateModelAndAppointment.appointmentList.filter(...)
);
will not perform any filtering of the top array because Array.prototype.filter
always returns an array and your code is thus filtering on the truthy-ness of []
, which is always truthy, and nothing gets filtered. 不会对顶部数组进行任何过滤,因为
Array.prototype.filter
总是返回一个数组,因此您的代码正在根据[]
的真实性进行过滤,该值始终是真实的,并且没有任何内容被过滤。
selectedDateAndAppointmentList.filter(dateModelAndAppointment =>
dateModelAndAppointment.appointmentList.filter(...)
// ^ will always return true because appointmentList.filter always returns an array
);
Here's what's happening: 这是正在发生的事情:
// no filtering is happening because inner // filter always returns a truthy value console.log( [{ values: ["a", "b"] }, { values: ["c", "d"] }].filter(item => item.values.filter(val => val === "a") ) );
Instead, you should use Array.prototype.some
or Array.prototype.every
which return a boolean and not an array and thus can be used as a filtering condition: 相反,您应该使用
Array.prototype.some
或Array.prototype.every
,它们返回一个布尔值而不是一个数组,因此可以用作过滤条件:
selectedDateAndAppointmentList.filter(dateModelAndAppointment =>
dateModelAndAppointment.appointmentList.some(...)
// ^ this will return a boolean based on a nested array condition
)
For example: 例如:
// filtering now works because the inner filter // correctly returns a boolean value depending // on the filter condition console.log( [{ values: ["a", "b"] }, { values: ["c", "d"] }].filter(item => item.values.some(val => val === "a") ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.