Search code examples
backbone.jschaplinjs

Backbone/chaplinJS : How to check for key existence in object


{
                  "providerProperties": [
                    {
                      "key": "hotelnum",
                      "value": "123"
                    },
                    {
                      "key": "contract",
                      "value": "ABC"
                    },
                    {
                      "key": "Sequence",
                      "value": "1213"
                    },
                    {
                      "key": "lastFourDigit",
                      "value": "5454"
                    },
                    {
                      "key": "shop",
                      "value": "abc"
                    },
                    {
                      "key": "bookingEmailAddress",
                      "value": "abc@gmail.COM"
                    },
                    {
                      "key": "numOfRooms",
                      "value": "1"
                    }
}

Backbone/chaplinJS : How to check for key existence in object. for example want to check of key "numOfRooms" is available or not.


Solution

  • You can use underscore method findWhere like _.findWhere(yourObject.providerProperties, {key: "numOfRooms"}); if the result is not undefined then it is present