Uploaded image for project: 'Qbs ("Cubes")'
  1. Qbs ("Cubes")
  2. QBS-1227

Add Array.prototype.find() from ECMAScript 2015

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Fixed
    • Icon: P4: Low P4: Low
    • 2.0
    • 1.9.1
    • API: JavaScript
    • None
    • 087c22e17 (master)

      Qbs JavaScript engine supports methods such as some (check that some element matches the predicate), forEach (execute predicate for each element), every (check that all elements matches the predicate) etc. It does not support ECMAScript 2015 addition of `find` (return first element that matches the predicate). As per JavaScript documentation the workaround to define it ourselves and they even provide the implementation. However it would be nice if it was part of the engine as I believe it is quite useful and considerably shortens the code as otherwise one has to iterate over the array, check the value and return the element or break from the loop:

      var value;
      var array = [1,2,3,4,5];
      for(var i in array) {
          if(array[i] == 3) {
              value = array[i];
              break;
          }
      }
      

      vs

      var value = [1,2,3,4,5].find(function(element) { return element == 3; });
      

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            kandeler Christian Kandeler
            resurr3ction Michael Vlach
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes