# Array filter from a letter

**URL:** <https://discourse.nodered.org/t/array-filter-from-a-letter/52395>\
**Category:** Dashboard\
**Created:** [19 October 2021 06:49 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395 "2021-10-19T06:49:46Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![RitaSghe](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RitaSghe](https://discourse.nodered.org/u/RitaSghe)\
**Post date:** [19 October 2021 06:49 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/1 "2021-10-19T06:49:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 October 2021 07:01 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/2 "2021-10-19T07:01:39Z")

</div>

Use `indexOf` and `slice`

```auto
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...

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

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 October 2021 08:15 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/3 "2021-10-19T08:15:01Z")

</div>

> [@RitaSghe](#):
>
> Well, I'd like to see the item from the letter P (to follow this letter),

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.

---

<div class="post-metadata">

**Author:** ![RitaSghe](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RitaSghe](https://discourse.nodered.org/u/RitaSghe)\
**Post date:** [19 October 2021 09:25 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/4 "2021-10-19T09:25:54Z")

</div>

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 ......

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 October 2021 09:36 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/5 "2021-10-19T09:36:38Z")

</div>

I gave you a working example

> [@Steve-Mcl](#):
>
> ```auto
> 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...
> 
> ```auto
> msg.payload = msg.payload.slice(msg.payload.indexOf('C')); 
> return msg;
> 
> ```

Proof...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/163330bbebde8ec232808a3ed7200d9af186f429.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 October 2021 10:06 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/6 "2021-10-19T10:06:10Z")

</div>

> [@RitaSghe](#):
>
> My array (msg.payload) contains these values:
> 
> A, B, C, D, E, F, G ...

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

> [@RitaSghe](#):
>
> while I'd like to see D, E, F, G

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.

---

<div class="post-metadata">

**Author:** ![RitaSghe](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RitaSghe](https://discourse.nodered.org/u/RitaSghe)\
**Post date:** [19 October 2021 11:02 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/7 "2021-10-19T11:02:54Z")

</div>

@Steve-Mcl

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

---

<div class="post-metadata">

**Author:** ![RitaSghe](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RitaSghe](https://discourse.nodered.org/u/RitaSghe)\
**Post date:** [19 October 2021 11:04 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/8 "2021-10-19T11:04:29Z")

</div>

@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 ..

> [@JSON Array Filtering - Function Node](https://discourse.nodered.org/t/json-array-filtering-function-node/42827/2):
>
> See this post from earlier today which asks an almost identical question. [Filter Array of Objects](https://discourse.nodered.org/t/filter-array-of-objects/42808) [Edit] Note that if you are using the filter function you have to call filter on the Array, which is msg.payload.trains, not msg.payload.CA\_MSG, and you have to check for equality (at least I assume that is what you want) so the test is el.area\_id === "RT") [Edit] as pointed out below that should be el.CA\_MSG.area\_id

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 October 2021 11:19 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/9 "2021-10-19T11:19:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RitaSghe](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RitaSghe](https://discourse.nodered.org/u/RitaSghe)\
**Post date:** [19 October 2021 11:35 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/10 "2021-10-19T11:35:59Z")

</div>

I try .......

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

 ![img01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9fbb91f8fc35ab4510232d63ab7d442278cbb503.png)

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)

 ![img02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2d4d6d30240f36e9081b3bf1b6532947a1da5626.png)

---

<div class="post-metadata">

**Author:** ![RitaSghe](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RitaSghe](https://discourse.nodered.org/u/RitaSghe)\
**Post date:** [19 October 2021 11:50 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/11 "2021-10-19T11:50:54Z")

</div>

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 .......

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 October 2021 12:43 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/12 "2021-10-19T12:43:37Z")

</div>

> [@RitaSghe](#):
>
> msg.payload = msg.payload.filter(e =\> e.title2 \> "C")

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.

---

<div class="post-metadata">

**Author:** ![RitaSghe](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RitaSghe](https://discourse.nodered.org/u/RitaSghe)\
**Post date:** [19 October 2021 16:53 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/13 "2021-10-19T16:53:17Z")

</div>

Ok,

@Colin  
@Steve-Mcl

Thank you for help !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 November 2021 16:53 UTC](https://discourse.nodered.org/t/array-filter-from-a-letter/52395/14 "2021-11-18T16:53:39Z")

</div>

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