Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-99041

Add 'reduce' method to QML list type

    XMLWordPrintable

Details

    • All
    • 7e4b179430 (qt/qtdeclarative/dev) 7e4b179430 (qt/tqtc-qtdeclarative/dev)

    Description

      JavaScript arrays have a reduce method that allows one to condense an array down into a single value, given a reducer function.

      It would be nice to have a reduce method on the QML list type, which would do the same thing, only operating on QML lists instead of JavaScript arrays.

      Suggested syntax would be exactly the same as the JavaScript Array.prototype.reduce method.

      An example that I was expecting to work is below and attached:

      import QtQuick
      import QtQuick.Controls
      import QtQuick.Layouts
      
      ApplicationWindow {
          width: 640
          height: 480
          visible: true
      
          ColumnLayout {
              anchors.fill: parent
      
              Component.onCompleted: {
                  var words = [ "Hello", "World"];
                  function stringLengthReducer(prev, curr) {
                      return prev + curr.length;
                  }
                  console.log("Array [\"Hello\", \"World\" length: " + words.reduce(stringLengthReducer, 0));
                  function textTextLengthReducer(prev, curr) {
                      return prev + curr.text.length;
                  }
                  var childrenTextLengthSum = children.reduce(textTextLengthReducer, 0);
                  console.log("Sum of lengths of Text children text strings: " + childrenTextLengthSum);
              }
      
              Text {
                  text: "Hello"
              }
              Text {
                  text: "World"
              }
          }
      }
      

      Attachments

        1. listreduce.qml
          0.9 kB
          Keith Kyzivat

        Issue Links

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

          Activity

            People

              ulherman Ulf Hermann
              kkyzivat Keith Kyzivat
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes