Astronaut
Astronaut

Reputation: 7051

How to store multiple key value, where value is an array of elements in javascript?

I am looking for a quick way to crate a key values pair in JavaScript, where I have a key it's an ID, like 1, 2, 3, 4, 5, .... 100 and the values are an array composed by a reference, name and quantity.

Is this an appropriate syntax?

var petfoodRefData = {
    "1": [
            {"ref":5222, "description":"purina", "qtt":500},
            {"ref":5322, "description":"hills", "qtt":500},
            {"ref":6222, "description":"hills junior", "qtt":500}
        ],
    "2": {"ref":8022, "description":"fatcat", "qtt":60}
}

Is there an easier better way to do this? I basically want to for every ID = 1 give me all pet food references, and add them to the document.

Upvotes: 0

Views: 615

Answers (1)

Jacob
Jacob

Reputation: 78920

You're close:

var petfoodRefData = {
    "1": [
        {"ref":5222, "description":"purina", "qtt":500},
        {"ref":5322, "description":"hills", "qtt":500},
        {"ref":6222, "description":"hills junior", "qtt":500}
    ],
    "2": [{ "ref":8022, "description":"fatcat", "qtt":60} ]
};

Use [] for arrays.

Upvotes: 2

Related Questions