1

I know I can do something like that:

var data = [
    {
        name: 'test1',
        value: 1
    }
];

But I want to fill data with dynamic values (from HTML). So I will have var data = []; and how can I fill this one? I know, it must be somewhere on the internet, but I don not know the "right words" to search.

Note: data have to be array filled with objects.

2 Answers 2

2

You can use the 'push' method.

var a = [];

a.push({item: 'a', value : 'a'});

console.log(a);

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

2 Comments

In your example there is error, you can not use = in object literal
When I saw your answers, I already realised that .push method, applied and worked well, so first time I did not see that =.
1

You can use .push method, like so

var data = [];

data.push({ name: 'test1',  value: 1});
data.push({ name: 'test2',  value: 2});
data.push({ name: 'test3',  value: 3});

console.log(data);

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.