# Filter Array of Objects

**URL:** <https://discourse.nodered.org/t/filter-array-of-objects/42808>\
**Category:** General\
**Created:** [18 March 2021 10:46 UTC](https://discourse.nodered.org/t/filter-array-of-objects/42808 "2021-03-18T10:46:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![David2608](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@David2608](https://discourse.nodered.org/u/David2608)\
**Post date:** [18 March 2021 10:46 UTC](https://discourse.nodered.org/t/filter-array-of-objects/42808/1 "2021-03-18T10:46:59Z")

</div>

Hello together,

I'm new to Node-Red and I have a comprehension problem. I dont understand how to filter an array of objects based on an object value.

From a MSSQL-Database i receive an array and now i want to split the array based on a specific object value:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cddbe5d9d12b218c497ecc966d1f1c91e86523aa.png)

I just want to have an array with all entries which have the value "SPANN" in "Kürzel".

I have seen a lot of approaches but i dont know what ist the best one.

Can anyone give me advise?

Thanks.

David

---

<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:** [18 March 2021 10:50 UTC](https://discourse.nodered.org/t/filter-array-of-objects/42808/2 "2021-03-18T10:50:38Z")

</div>

> [@David2608](#):
>
> I have seen a lot of approaches but i dont know what ist the best one.

The best approach is to filter in your SQL.

`SELECT * from table where Kürzel = 'SPANN'`

This avoids pulling unnecessary records from the database and unnecessary post-processing/filtering.

* * *

But if you must, then use [array.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) in a function

e.g...

```auto
msg.payload = msg.payload.filter(e => e.Kürzel == 'SPANN');
return msg;

```

---

<div class="post-metadata">

**Author:** ![David2608](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@David2608](https://discourse.nodered.org/u/David2608)\
**Post date:** [18 March 2021 11:13 UTC](https://discourse.nodered.org/t/filter-array-of-objects/42808/3 "2021-03-18T11:13:39Z")

</div>

Thank you for the prompt reply!

The filter function worked for me bcause i want to use several filter for different dashboard groups.

---

<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:** [18 March 2021 11:45 UTC](https://discourse.nodered.org/t/filter-array-of-objects/42808/4 "2021-03-18T11:45:27Z")

</div>

If the columns you want to filter on are indexed then it would probably be more efficient to do multiple calls to the database rather then multiple passes through the dataset testing for different values. The database code is highly optimised for that task. On the other hand if your system is lightly loaded then it doesn't make much difference.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 March 2021 12:44 UTC](https://discourse.nodered.org/t/filter-array-of-objects/42808/5 "2021-03-18T12:44:52Z")

</div>

You can do multiple selects in one query also  
eg

```auto
(SELECT
   *
    FROM table WHERE Kurzel = 'SPANN') AS table_a,

 (SELECT
    *
    FROM table WHERE Kurzel = 'ANOTHER') AS table_b

```

> <https://stackoverflow.com/questions/1775168/multiple-select-statements-in-single-query>

---

<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:** [18 March 2021 12:54 UTC](https://discourse.nodered.org/t/filter-array-of-objects/42808/6 "2021-03-18T12:54:35Z")

</div>

I dont think syntax will work for MS-SQL but you can do the same thing...

```sql
SELECT * FROM table WHERE Kurzel = 'SPANN') AS table_a
SELECT * FROM table WHERE Kurzel = 'ANOTHER') AS table_b

```

Results are returned in an array as shown below...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca06b0d4c7774c1013c8c8132a0e7a06d1326da5.png)

The OP would need to be using [node-red-contrib-mssql-plus](https://flows.nodered.org/node/node-red-contrib-mssql-plus)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 March 2021 12:56 UTC](https://discourse.nodered.org/t/filter-array-of-objects/42808/7 "2021-03-18T12:56:40Z")

</div>

Miss read the post thought it said mysql.

---

<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:** [1 April 2021 12:56 UTC](https://discourse.nodered.org/t/filter-array-of-objects/42808/8 "2021-04-01T12:56:44Z")

</div>

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