How to Push multiple Values to an Array in a single time
-
wrote on 10 Feb 2022, 07:27 last edited by
Hi Team,
I am having an issue to Push multiple Values to an Array in a single time.
I am not good in QML programming.
Can someone please help me on this.The ouput from the below code is
qml: Push = [object Object],[object Object]
qml: Pop = [object Object]Please find below sample code
Window { visible: true width: 640 height: 480 title: qsTr("Hello World") property var list: [] Component.onCompleted: { list.push({"A001": "1"}) console.log("Push = " + list) //expecting output to be {"A001","1"} list.push({"A002": "2"}) console.log("Push = " + list) //expecting output to be {"A001","1"}, {"A002","2"} list.pop() console.log("Pop = " + list) //expecting output to be {"A001","1"} } }
-
wrote on 10 Feb 2022, 15:11 last edited by lemons 2 Oct 2022, 15:31
if you expect JSON key-value pairs as output, you have to push those as JSON objects to the list
Window { visible: true width: 640 height: 480 title: qsTr("Hello World") property variant list: [] property string str: "A001" property int value: 1 // option 2 property var object: ({}) Component.onCompleted: { // option 1 - do it in js only let obj = {} obj[str] = value list.push(obj) console.log(JSON.stringify(list[0])) //expecting output to be {"A001",1} // option 2 - use property object[str] = value list.push(object) console.log(JSON.stringify(list[1])) //expecting output to be {"A001",1} // note if value should be of type string: object[str] = value.toString() list.push(object) console.log(JSON.stringify(list[2])) //expecting output to be {"A001","1"} } }
-
wrote on 10 Feb 2022, 09:41 last edited by
As your list entries are JSON objects, you have to stringify them before logging.
Try this instead:Component.onCompleted: { list.push({ "A001": "1" }) // note that you can simply separate the log outputs with commas // no need for string concatenation console.log("Push =", JSON.stringify(list)) list.push({ "A002": "2" }) console.log("Push =", JSON.stringify(list)) list.pop() console.log("Push =", JSON.stringify(list)) // pushing multiple entries at once list.push({ "A002": "2" }, { "A003": "3" }, { "A004": "4" }, { "A005": "5" }) console.log("Push =", JSON.stringify(list)) }
-
As your list entries are JSON objects, you have to stringify them before logging.
Try this instead:Component.onCompleted: { list.push({ "A001": "1" }) // note that you can simply separate the log outputs with commas // no need for string concatenation console.log("Push =", JSON.stringify(list)) list.push({ "A002": "2" }) console.log("Push =", JSON.stringify(list)) list.pop() console.log("Push =", JSON.stringify(list)) // pushing multiple entries at once list.push({ "A002": "2" }, { "A003": "3" }, { "A004": "4" }, { "A005": "5" }) console.log("Push =", JSON.stringify(list)) }
wrote on 10 Feb 2022, 12:37 last edited by@lemons Thank you for your quick response and the solution provided.
But, if I pass the variable names to the push method, the console output is different like below
qml: [{"str":"A001","value":1}]I am expecting output to be {"A001","1"}
How can I do this ?Window { visible: true width: 640 height: 480 title: qsTr("Hello World") property variant list: [] property string str: "A001" property int value: 1 Component.onCompleted: { list.push({str, value}) console.log(JSON.stringify(list)) //expecting output to be {"A001","1"} } }
-
wrote on 10 Feb 2022, 15:11 last edited by lemons 2 Oct 2022, 15:31
if you expect JSON key-value pairs as output, you have to push those as JSON objects to the list
Window { visible: true width: 640 height: 480 title: qsTr("Hello World") property variant list: [] property string str: "A001" property int value: 1 // option 2 property var object: ({}) Component.onCompleted: { // option 1 - do it in js only let obj = {} obj[str] = value list.push(obj) console.log(JSON.stringify(list[0])) //expecting output to be {"A001",1} // option 2 - use property object[str] = value list.push(object) console.log(JSON.stringify(list[1])) //expecting output to be {"A001",1} // note if value should be of type string: object[str] = value.toString() list.push(object) console.log(JSON.stringify(list[2])) //expecting output to be {"A001","1"} } }
-
if you expect JSON key-value pairs as output, you have to push those as JSON objects to the list
Window { visible: true width: 640 height: 480 title: qsTr("Hello World") property variant list: [] property string str: "A001" property int value: 1 // option 2 property var object: ({}) Component.onCompleted: { // option 1 - do it in js only let obj = {} obj[str] = value list.push(obj) console.log(JSON.stringify(list[0])) //expecting output to be {"A001",1} // option 2 - use property object[str] = value list.push(object) console.log(JSON.stringify(list[1])) //expecting output to be {"A001",1} // note if value should be of type string: object[str] = value.toString() list.push(object) console.log(JSON.stringify(list[2])) //expecting output to be {"A001","1"} } }
wrote on 10 Feb 2022, 16:06 last edited by@lemons
Thanks again and Sorry for troubling you again
I understood, how to store the key value pair in the list using json object.
But, one thing that is disturbing in my mind is, if I want to push 3 or 4 arguments to the list like below
({"A001", 1, "Item1"}, {"A002", 2, "Item2"})
How can I do this ? I know these are the basic things in js, but can't figure out the solution
1/5