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 }
];
and then access it:
alert(list[1].date);
Answer from Darin Dimitrov on Stack Overflowvar 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 }
];
and then access it:
alert(list[1].date);
dynamically build list of objects
var listOfObjects = [];
var a = ["car", "bike", "scooter"];
a.forEach(function(entry) {
var singleObj = {};
singleObj['type'] = 'vehicle';
singleObj['value'] = entry;
listOfObjects.push(singleObj);
});
here's a working example http://jsfiddle.net/b9f6Q/2/ see console for output
how to create list of objects in javascript - Stack Overflow
How to create a list of class objects
arrays - Creating a list of objects inside a javascript Object - Stack Overflow
Get a list of all the properties of an object?
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
});
}
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'/>
I am attempting to have a list of class objects within JavaScript. I have done in Flutter but seems to be a tad bit different in JavaScript. I have a class object
class Sections{
constructor(lecture, quiz, assignment, name){
this.lecture = lecture;
this.quiz = quiz;
this.assignment = assignment;
this.name = name;
}
}
This class will have multiple instances within one Chapter. so a list of this class needs to be created. Normally you would say List<Sections> VarName = []; but, in JavaScript the list variable type does not seem to be a thing. So does anyone know how to accomplish this?
To create the structure you mention you can write it in JSON like this:
{
"assignToMap": {
"123": [1, 2, 3, 4],
"345": [1, 2, 3, 4],
"678": [1, 2, 3, 4]
}
}
Although I'm not entirely sure if that's what you're asking here!
I believe you are talking about an associative array. Use the curly braces. This is an array of associative arrays.
var myArray = [
{ myKey1: "myValue1", myKey2: "myValue2", myKey3: "myValue3" },
{ myKey4: "myValue4", myKey5: "myValue5", myKey6: "myValue6" },
{ myKey7: "myValue7", myKey8: "myValue8", myKey9: "myValue9" }
]
So I made an array with objects. For example here, let's use people
var people = [
{
id: 0,
name: "John Doe",
age: 47
},
{
id: 1,
name: "Jane Doe",
age: 88
},
{
id: 2,
name: "Mason Louis",
age: 17
}
];
so now I can use people[1].name etc for accessing the data on each of these people. Someone just saw my code and said "That looks like total S**T".
What is the proper way to do this? I was thinking this way was fine, especially since it resembles JSON
EDIT: Would it be better to create a constructor and fill an array with instances?