Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create list of objects in javascript

Tags:

javascript

        var employee =
        {
            Column1: null,
            Column2: null,

            create: function () {
                var obj = new Object();

                obj.Column1 = "";
                obj.Column2 = "";

                return obj;
            }
        };

In C# I would do something like this:

List<Employee> employees = new List<Employee>();

for (int i = 0; i < 10; i++)
{
    Employee emp = new Employee()
    {
        Column1 = "column 1 of emp" + i;
        Column2 = "column 2 of emp" + i;
    }
    employees.Add(emp);
}

I need to do the same in javascript.

like image 263
GaneshT Avatar asked Dec 04 '22 11:12

GaneshT


1 Answers

Pretty straight forward approach to creating an array of objects.

var employees = [];

for (var i = 0; i < 10; i++) {
    employees.push({
        Column1: 'column 1 of emp' + i,
        Column2: 'column 1 of emp' + i
    });
}
like image 59
Matthew Cox Avatar answered Dec 25 '22 18:12

Matthew Cox