Reputation: 171
I am a newbie and I am trying to create a new array from an existing array but only keep the object when a property of the object doesn't appear in another array...
ticketDetailsSubset2 = ticketDetailsArray.filter((i) => {
!excludeTickets.includes(i["stl19:TicketNumber"][0]);
});
If the i["stl19:TicketNumber"][0]
is in the exclude Tickets array then don't include it in the new array...
Here's the array of exclude Tickets..
[Array(1)]0: ["0017522827238"]
The array of ticket Details Array is pretty big but here's a snippet...
0: {$: {…}, stl19:OriginalTicketDetails: Array(1), stl19:TransactionIndicator: Array(1), stl19:TicketNumber: Array(1), stl19:PassengerName: Array(1), …}
1: {$: {…}, stl19:OriginalTicketDetails: Array(1), stl19:TransactionIndicator: Array(1), stl19:TicketNumber: Array(1), stl19:PassengerName: Array(1), …}
2:
$: {id: "90", index: "3", elementId: "pnr-90"}
stl19:AgencyLocation: ["29JB"]
stl19:AgentSine: ["A46"]
stl19:DutyCode: ["*"]
stl19:OriginalTicketDetails: ["TE 0017522827239-AT YOASH/N 29JB*A46 1439/16APR "]
stl19:PassengerName: ["CLAUS/M"]
stl19:PaymentType: [""]
stl19:TicketNumber: ["0017522827239"]
stl19:Timestamp: ["2021-04-16T14:39:00"]
stl19:TransactionIndicator: ["TE"]
The problem is the array returned has no rows. At least one of the stl19:TicketNumber is NOT in the exclude Tickets array and therefore should be returned. If the condition is false I wanted the object returned. There's no error but it's not evaluating the filter with the condition clause. I'm a beginner so sorry if this is a js 101 question.
Upvotes: 0
Views: 51
Reputation: 26
The filter function expects to receive a function in the parameter that returns a boolean.
If you want to know more about the filter function click here
Looking at your code, we can see that you passed a void callback function (which has no return), I believe that the example below solves your problem:
ticketDetailsSubset2 = ticketDetailsArray.filter((i) => {
return !excludeTickets.includes(i["stl19:TicketNumber"][0]);
});
You can also use an arrow function, sample code:
ticketDetailsArray.filter((i) => !excludeTickets.includes(i["stl19:TicketNumber"][0]));
Upvotes: 1