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.
             
            
              
            
           
          
            
              
                Colin  
              
                  
                    18 March 2021 11:45
                   
                  4 
               
             
            
              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.
             
            
              
            
           
          
            
              
                E1cid  
              
                  
                    18 March 2021 12:44
                   
                  5 
               
             
            
              You can do multiple selects in one query also
(SELECT
   *
    FROM table WHERE Kurzel = 'SPANN') AS table_a,
 (SELECT
    *
    FROM table WHERE Kurzel = 'ANOTHER') AS table_b
  
  
      
     
  mysql 
   
  
    
    
  
  
 
             
            
              
            
           
          
            
            
              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 
             
            
              
            
           
          
            
              
                E1cid  
              
                  
                    18 March 2021 12:56
                   
                  7 
               
             
            
              Miss read the post thought it said mysql.
             
            
              
            
           
          
            
              
                system  
              
                  
                    1 April 2021 12:56
                   
                  8 
               
             
            
              This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.