Filter Array of Objects

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:

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

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 in a function

e.g...

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

Thank you for the prompt reply!

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

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.

You can do multiple selects in one query also
eg

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

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

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

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

The OP would need to be using node-red-contrib-mssql-plus

Miss read the post thought it said mysql.

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