4
        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.

2

3 Answers 3

11

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
    });
}
Sign up to request clarification or add additional context in comments.

Comments

6

It's a Old question but just wanna contribute to it. I used Arrays

function car(brand, color, year, price) {
    this.brand = brand;
    this.color = color;
    this.year = year;
    this.price = price;
}

var my = new Array();
my.push(new car("Ford", "Black", 2017, 15000));
my.push(new car("Hyundai", "Red", 2017, 17000));

document.getElementById('ford').value = my[0].price;
document.getElementById('hyundai').value = my[1].price;
Ford price: <input type="text" id='ford'/><br><br>
Hyndai price: <input type="text" id='hyundai'/>

Comments

2
var list = [
{ date: '12/1/2011', reading: 3, id: 20055 },
{ date: '13/1/2011', reading: 5, id: 20053 },
{ date: '14/1/2011', reading: 6, id: 45652 }
];

for accessing the list use :

list[index].date

1 Comment

Is that the answer to a previous version of the question?

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.