# Array group by text based on certain character

**URL:** <https://discourse.nodered.org/t/array-group-by-text-based-on-certain-character/76982>\
**Category:** General\
**Created:** [27 March 2023 10:57 UTC](https://discourse.nodered.org/t/array-group-by-text-based-on-certain-character/76982 "2023-03-27T10:57:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 March 2023 10:57 UTC](https://discourse.nodered.org/t/array-group-by-text-based-on-certain-character/76982/1 "2023-03-27T10:57:22Z")

</div>

I have been in a fight with an array which is similar to (original contains 5000+ elements):

```auto
[
    "Some Section ⌄",
    "b",
    "c",
    "d",
    "e",
    "Some Other section ⌄",
    "b",
    "c",
    "d",
    "x",
]

```

I want to produce an output that looks like:

```auto
[
	{
		"section": "Some Section", 
		"items":[ 
			"b",
    		"c",
    		"d",
    		"e"
    	]
	},
	{
		"section": "Some Other Section", 
		"items":[ 
			"b",
    		"c",
    		"d",
    		"x"
    	]
	},

]

```

So the "breaking point" or "grouping character" is the ` ⌄` symbol. I tried to capture the index into a new array and then loop from index to index -1 etc. Whatever I attempt, I fail, does anyone have a magic trick up their sleeve ? I can no longer see it properly :')

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [27 March 2023 11:09 UTC](https://discourse.nodered.org/t/array-group-by-text-based-on-certain-character/76982/2 "2023-03-27T11:09:06Z")

</div>

This may work then..

```auto
let input = [
    "Some Section ⌄",
    "b",
    "c",
    "d",
    "e",
    "Some Other section ⌄",
    "b",
    "c",
    "d",
    "x",
]

let output = [] 
let obj
input.forEach(item => {    
    if (item.includes('⌄')){
        obj = {}
        obj.section = item.slice(0, -1).trim();
        obj.items = []
        output.push(obj)
    }
    else{
        obj.items.push(item)
    }
})
msg.payload = output
return msg;

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 March 2023 11:23 UTC](https://discourse.nodered.org/t/array-group-by-text-based-on-certain-character/76982/3 "2023-03-27T11:23:34Z")

</div>

I swear that I had something similar, but it did not want to function like yours, thanks a lot! Can finally move forward.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [27 March 2023 11:27 UTC](https://discourse.nodered.org/t/array-group-by-text-based-on-certain-character/76982/4 "2023-03-27T11:27:19Z")

</div>

Just as it isn't protected against possible variations in incoming data, be prepared for errors 😃

---

<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:** [10 April 2023 11:27 UTC](https://discourse.nodered.org/t/array-group-by-text-based-on-certain-character/76982/5 "2023-04-10T11:27:38Z")

</div>

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