• (nodebb)

    Also, little pro-tip for generating comma separated code, and this is just a real tiny optimization: before the loop append item zero, start the loop with item 1, and then you can unconditionally prepend a comma, removing any conditional logic from your loop.

    Or if you're using a sane language, use the builtin join function.

  • (nodebb) in reply to Dragnslcr

    Or if you're using a sane language, use the builtin join function.

    Above all, this!

    Addendum 2026-02-18 07:36: And, curiously, despite all its many insanities, JS is sufficiently sane in this particular respect. Well, unless I've missed something about Array.prototype.join()

  • CodeRefactorer (unregistered)

    I have seen a lot of code where they always append the separator comma, and after the loop they take a substring of the result with all characters except the last. What they usually forget is the case when the array is empty and the for-loop is skipped. There it always crashes badly when taking the substring.

    Another way of doing it:

    let arrayString = '{';
    let separator = "";
      for(let i = 0; i < jsArray.length; i++) {
        arrayString += separator + jsArray[i];
        separator=",";
        }
      }
      arrayString += '}';
    
  • CodeRefactorer (unregistered)

    oh well, the newline did not work. And I can't correct my pevious post... Trying with two slashes instead of backslashes ... Another way of doing it: // let arrayString = '{'; // let separator = "";// for(let i = 0; i < jsArray.length; i++) { // arrayString += separator + jsArray[i];// separator=",";// }// arrayString += '}';//

  • (nodebb)

    separator=",";

    Yeah, that's how I do it.

  • (nodebb) in reply to CodeRefactorer

    ''' use three single quotes the line breaks will work for you it's also monospaced

    Addendum 2026-02-18 09:05: Well, that was wrong

  • (nodebb) in reply to CodeRefactorer
    typing three backquotes
    the line breaks will work for you
    it's also monospaced
    
  • (nodebb) in reply to CodeRefactorer

    You could also use a ternary:

    let arrayString = '{';
    for(let i = 0; i < jsArray.length; i++) {
      arrayString += (i ? ',' : '') + jsArray[i];
    }
    arrayString += '}';
    

    But as @Dragnslcr said, join() is the better way, and as @Steve_The_Cynic mentioned, JavaScript has it built-in:

    let arrayString = '{' + jsArray.join(',') + '}';
    
  • (nodebb)

    I'll also post the periodic reminder that the comments here now render Markdown, so you can use that for quotes, code, etc.

  • Kotarak (unregistered)

    I don't even understand how that works.

          returnQueryParams.push(`${key} @> ?`);
          returnQueryParams.push(exports.buildArrayString(values));
    

    I assume the second push is used to fill the ? in the first. However, doesn't that mean it is interpreted as a string? Not as an array? I mean: it's a string.

    And a broken one at that. How should Knex or Postgres know, that this array ["1", "2, 3", "4" ] is supposed to have three elements? Maybe not Little Bobby Tables broken, but broken nonetheless.

  • Vilx- (unregistered)

    My preference is to use an ORM for all the common, tedious cases (most inserts, updates and deletes fall in this category, as well as simple selects), and for the rest drop to plain old SQL with parameters that are automatically escaped. I've never understood the appeal of query generators - you just end up with a mess of a code which basically parallels an SQL query, but is much more verbose and hard to read. There might be some projects that benefit from SQL-dialect-agnosticism that a query builder provides, but 99% of all software out there makes the DB choice first and never switches from that. Plus when you start to use an SQL-dialect-agnostic code, you also LOSE access to all the specific features that might be available in your particular dialect. Like that @> operator.

Leave a comment on “Contains Some Bad Choices”

Log In or post as a guest

Replying to comment #:

« Return to Article