Tyler M
Tyler M

Reputation: 412

How to check if JSON object parameter exists?

I have a ListModel of objects which may or may not have a myvis parameter (used for visibility). The ListView delegate should be visible if myvis === true. If myvis was not defined, I want to assume that myvis === true

For checking if myvis was defined and/or exists, I've tried 3 approaches:

None of which have been successful; see code output below

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480

    ListModel {
        id: myjson
        ListElement {
            displayName: "Planes"
            myvis: false
        }
        ListElement {
            displayName: "Trains"
            myvis: true
        }
        ListElement {
            displayName: "Automobiles"
        }
    }


    ListView {
        model: myjson
        anchors.fill: parent

        delegate: Text {
            text: displayName
            visible: {
                console.log("typeof " + displayName + ":myvis is " + (typeof myvis))
                console.log("hasOwnProperty " + hasOwnProperty("myvis"))
                console.log(displayName + " is " + myvis)

                //if myvis is not defined, assume it to be TRUE
                return (hasOwnProperty("myvis") ? myvis : true)
            }
        }
    }
}

Output I'm getting:

qml: typeof Planes:myvis is boolean
qml: hasOwnProperty false
qml: Planes is false
qml: typeof Trains:myvis is boolean
qml: hasOwnProperty false
qml: Trains is true
qml: typeof Automobiles:myvis is boolean
qml: hasOwnProperty false
qml: Automobiles is false

Output I'd expect:

qml: typeof Planes:myvis is boolean
qml: hasOwnProperty true
qml: Planes is false
qml: typeof Trains:myvis is boolean
qml: hasOwnProperty true
qml: Trains is true
qml: typeof Automobiles:myvis is undefined
qml: hasOwnProperty false
qml: Automobiles is undefined

EDIT If the model is a JSON object and non-QML then it works:

    property var myjson: [
        {
            displayName: "planes",
            myvis: false
        },
        {
            displayName: "trains",
            myvis: true
        },
        {
            displayName: "automobiles"
        }
    ]

    ListView {
        model: myjson
        anchors.fill: parent

        delegate: Text {
            text: modelData.displayName
            visible: {
                console.log("typeof " + modelData.displayName + ":myvis is " + (typeof modelData.myvis))
                console.log("hasOwnProperty " + modelData.hasOwnProperty("myvis"))
                console.log(modelData.displayName + " is " + modelData.myvis)

                //if myvis is not defined, assume it to be TRUE
                return (modelData.hasOwnProperty("myvis") ? modelData.myvis : true)
            }
        }
    }

Output is exactly as expected. Is there a way to get this output while retaining the ListModel and ListElements?

Upvotes: 1

Views: 1265

Answers (1)

Cuneyt Celebican
Cuneyt Celebican

Reputation: 46

What happens if you just set myvis !== false I think this is what you are looking for. You just need to show your thing if it is not equal to false.

Upvotes: 1

Related Questions