0

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]);
          });
  • ticketDetailsArray=the main array
  • ticketDetailsSubset2= the new array I want to create
  • excludeTickets=the array containing ticket numbers..

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.

1
  • 1
    missing return statem with curly brackets, it should be (i) => !excludeTickets.includes(i["stl19:TicketNumber"][0]) without curly or with return statement and curly brackets. Commented Apr 18, 2021 at 12:53

1 Answer 1

1

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]));
Sign up to request clarification or add additional context in comments.

4 Comments

In this case it is difficult to know if the signature of the object is correct, but considering that it is already using this code, I am assuming that it is. In the case of the code reported by the author, what is missing is the return in the function
checking 0017522827238 and return value=false checking 0017522827239 and return value=false ``` I changed it to this. ticketDetailsSubset2 = ticketDetailsArray.filter((i: any) => { console.log( checking ${ i["stl19:TicketNumber"][0] } and return value=${excludeTickets.includes( i["stl19:TicketNumber"][0] )} ); return !excludeTickets.includes(i["stl19:TicketNumber"][0]); }); ``` Why is the return value false for "0017522827238". it's in the exclude
whoops, sorry don't know how to make the comment code in a reply obviously-duh
Your direction sounds like it would work but it's not working. Which is strange. I will re-post and try and better demonstrate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.