locked
Filtering query result with helper function RRS feed

  • Question

  • Hi,

    what I'm trying to do is query some data and then filter it using a helper function on each result. After that i want to respond with the remaining results. Here's the code for it:

    mssql.query(sqlQuery, { success: function (results) { results.forEach(function (r) { if (myfunction(r) < 50) { // KEEP } else { // REMOVE FROM RESULTS. } })

    request.respond(200, results); }, error: function () { request.respond(statusCodes.BAD_REQUEST, 'Some error messsage goes here.'); } });


    Is there any way to do this? Thank's for the help!

    Tuesday, April 29, 2014 6:51 AM

Answers

  • Yes, you can change the value of 'results' prior to returning (via request.respond) to the caller, as shown below.

    mssql.query(sqlQuery, {
        success: function (results) {
            for (var i = results.length - 1; i >= 0; i--) {
                if (myFunction(results[i])) {
                    // keep item, do nothing
                } else {
                    results.splice(i, 1); // remove item
                }
            });
    
            request.respond(200, results);
        }, error: function () {
            request.respond(statusCodes.BAD_REQUEST, 'Some error messsage goes here.');
        }
    });


    Carlos Figueira


    Tuesday, April 29, 2014 1:55 PM