Soy nuevo en las tecnologías qml y javascript. Necesito un ejemplo de ejemplo sobre operaciones push y pop en la matriz. Por ejemplo, ¿cómo puedo insertar 3 variables en la lista de matrices en QML a la vez? Quiero que la salida sea como la siguiente [{"A001", 1, "Item1"}, {"A002", 2, "Item2"}]. Comencé con el código de muestra, pero no pude completar la lógica en Component.onCompleted.
Window { visible: true width: 640 height: 480 title: qsTr("Hello World") property var object: ({}) property var list: [] property string str1: "A001" property int value1: 1 property string item1: "Item1" property string str2: "A002" property int value2: 2 property string item2: "Item2" Component.onCompleted: { //1. Frame the object {str1, value1, item1} //2. Push to the array using push operation //3. The output of the array to be [{"A001", 1, "Item1"}] //4. Frame the object {str2, value2, item2} //5. Push to the array using push operation //6. The output of the array to be [{"A001", 1, "Item1"},{"A002", 2, "Item2"}] //7. Do pop operation on the array //8. The output of the array to be [{"A001", 1, "Item1"}] //9. Do pop operation on the array //10. The output of the array to be [] }}
Parece que quieres una matriz de objetos. En javascript, los objetos son pares clave/valor. Así que tienes que crearlos algo como esto:
var obj1 = {"str": str1, "value": value1, "item": item1} var obj2 = {"str": str2, "value": value2, "item": item2}Luego puede empujarlos o colocarlos en su matriz:
var arr = []; arr.push(obj1); arr.push(obj2); arr.pop(); arr.pop();