Details
-
Suggestion
-
Resolution: Done
-
P2: Important
-
6.2.2
-
-
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
Issue Links
- is cloned by
-
QTBUG-99145 Add 'filter' method to QML list type
-
- Open
-
- is required for
-
QTBUG-104706 qmllint does not recognize JS array functions on QQmlListProperty
-
- Closed
-