I'm struggling to create an array filter that can handle exact and partial data within a nested array structure. The challenge is maintaining the integrity of the top-level structure while filtering based on data in the second layer. Here's an example json:
{
OpenClientIndicator: false,
listTypeCreditOffer: [{
companyName: 'itau',
offerCard: [{
nameCard: 'black',
idCard: 10,
availableValue: 10000.00
},{
nameCard: 'platinum',
idCard: 9,
availableValue: 5000.00
},....]
id:1
},{
companyName: 'santander',
offerCard: [{
nameCard: 'black',
idCard: 100,
valor: 15000.00
},{
nameCard: 'platinum',
idCard: 90,
availableValue: 7000.00
},....]
id:2
},...]
}
The filter criteria are based on nameCard and id Card, while preserving the original structure. For instance, if the filter value is 10, the expected output should be as follows:
{
OpenClientIndicator: false,
listTypeCreditOffer: [{
companyName: 'itau',
offerCard: [{
nameCard: 'black',
idCard: 10,
availableValue: 10000.00
}
id:1
]
}
However, I'm encountering a problem where the current structure only processes the first record it matches and doesn't scan through all arrays in the first layer.
let cards;
const newDados = jsonArray.listTypeCreditOffer.forEach((offer) => {
cards = offer.offerCard.filter((card) => {
if( card.idCard.toString().includes(filterValue.toUpperCase()) ||
card.nameCard.toUpperCase().includes(filterValue.toUpperCase())) {
return card;
}
});
});
This is where I need to refine my logic to address this issue.