Luke G
Luke G

Reputation: 1747

Associative Arrays - accessing parameters

I have the following object and string variable passed in vue's props:

catId :

"51bd55834e8d79bfd458ff8a156e3c11"

categories :

{
    "51bd55834e8d79bfd458ff8a156e3c11": {
        "header": "Header One",
        ".key": "51bd55834e8d79bfd458ff8a156e3c11"
    },
    "eb4312829f9c57b55727b4d8c6ca1ec7": {
        "typeId": "ba86f15d5b2126ac3d0ad39feeb5f400",
        "header": "Header Two",
        ".key": "eb4312829f9c57b55727b4d8c6ca1ec7"
    }
}

How can I access the header parameter of the given category?

I tried the following...

{{categories[this.catId].header}}

...but it does not work.

Upvotes: 0

Views: 39

Answers (2)

Husam Elbashir
Husam Elbashir

Reputation: 7187

Seems that you're using text interpolation with mustaches. In that case I don't think you need this. Try ..

{{categories['catId'].header}}

Upvotes: 3

Jerodev
Jerodev

Reputation: 33206

Seems to work fine.

var id = "51bd55834e8d79bfd458ff8a156e3c11"
var categories = {
    "51bd55834e8d79bfd458ff8a156e3c11": {
        "header": "Header One",
        ".key": "51bd55834e8d79bfd458ff8a156e3c11"
    },
    "eb4312829f9c57b55727b4d8c6ca1ec7": {
        "typeId": "ba86f15d5b2126ac3d0ad39feeb5f400",
        "header": "Header Two",
        ".key": "eb4312829f9c57b55727b4d8c6ca1ec7"
    }
};
console.log(categories[id].header);

Upvotes: 1

Related Questions