Array filter from a letter

Hi,
I set one filter in my array with this code:
msg.payload = msg.payload.filter(e => e.title2.includes("P"))
and I see my list with all the item start with letter P.

Well, I'd like to see the item from the letter P (to follow this letter), not only with letter P ....., so, P,Q,R,S,T ...........

Can you help me?

Use indexOf and slice

const letters = ['A', 'B', 'C', 'D', 'E']; //msg.payload
var pos = letters.indexOf('C');
msg.payload = letters.slice(pos); // C,D,E
return msg;

or shorthand...

msg.payload = msg.payload.slice(msg.payload.indexOf('C')); 
return msg;

I don't understand. Can you give an example of what you mean.

Whatever you mean, can you not just add it to the function in the filter? Use the syntax with a separate filter function if the function gets complex as that makes it easier to follow.

It's my fault and of my bad English, but I try to explain better ....

My array (msg.payload) contains these values:

A, B, C, D, E, F, G .....

with this filter:
msg.payload = msg.payload.filter(e => e.title2.includes("D"))

I see in my dashboard list only the value D

while I'd like to see D, E, F, G .....

I don't understand how to set my filter ......

I gave you a working example

Proof...

image

Presumably you mean msg.payload.title contains that. So you have in message.payload.title
["A","B","C",....]

So you want to drop each element of the array until you get to the matching one, then include all the ones physically after that in the array up to the end of the array? Or do you mean you want to include all elements that are alphabetically after "D"? So if the array were
["E","A","D","C",....]
then you would drop the A and C but but keep the first one "E" and all elements alphabetically after "D" (and including "D"?

If I still haven't got it then show us real example of the incoming data and exactly what you want to get out.

@Steve-Mcl

Yes, yes ....
I tried your code and works fine, of course,
but the problem is adapt your code to my array .....

@Colin
no, msg.payload is my array that contains more value like title, title2, isCheked, icon
and I try to filter by value "title2" to include all elements that are alphabetically after "D", (including "D"), so D, E, F, G .....

In this thread ..

there is one your post for the filter I use (modified for my values of course)

Could you provide sample data (use the copy value button appears in the your mouse when you have a over the payload in the debug sidebar)

And

What you expect the final result to look like.

Without this, we are guessing.

I try .......

In the following image you can see my array with all my values .....
I have values for all the alphabet ..... (title2)

In the following image instead you can see my array with
the filter msg.payload = msg.payload.filter(e => e.title2.includes("C"))
and you can see all the values start with the letter C (only with letter "C"), while I'd like to see the values after "C" (including "C") (title2)

OMG,
it was enough modify the code from
msg.payload = msg.payload.filter(e => e.title2.includes("C"))
to
msg.payload = msg.payload.filter(e => e.title2 > "C")

and it works .......

You probably want
msg.payload = msg.payload.filter(e => e.title2 >= "C")
otherwise if you have a title of exactly "C" it will not find it.

Ok,

@Colin
@Steve-Mcl

Thank you for help !

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.