1
"elements": [
    {
        "values": [
            {
                "value": 70
            }
        ],
        "dot-style": {
            "dot-size": 2,
            "halo-size": 2,
            "type": "solid-dot"
        },
        "on-show": {
            "type": ""
        },
        "font-size": 15,
        "loop": false,
        "type": "line",
        "tip": "#val#%"
    }
]

In the above array example I need to add data to values array which is part of elements array dynamically. How do I do it using JavaScript push method?

4 Answers 4

4

As you will see, it's much easier to conceptualise your code if it is formatted well. Tools like jsBeautifier can help with this task.

First, it's clear that elements is part of a JS object. We'll call it foo, but you'll have to change this to the correct name in your code.

foo.elements[0].values.push({
    value: 'some value'
});

This will add a new object to the values array.

Sign up to request clarification or add additional context in comments.

Comments

3
elements[0].values.push({"value": new_value});

Comments

1

if the above is named var obj,

obj['elements'][0]['values'].push(someValue);

Comments

1

Presuming elements is part of an object called myObj for the example below, you could use either syntax.

myObj["elements"][0]["values"].push({ value: "my new value" });

or

myObj.elements[0].values.push({ value: "my new value" });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.