Patrik Krehák
Patrik Krehák

Reputation: 2683

Add data to object array in javascript

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.

Upvotes: 1

Views: 62

Answers (2)

NeoPix
NeoPix

Reputation: 443

You can use the 'push' method.

var a = [];

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

console.log(a);

Upvotes: 2

Oleksandr T.
Oleksandr T.

Reputation: 77522

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);

Upvotes: 1

Related Questions