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
I was a gifted a copy of Eloquent JavaScript for Christmas, and I came across a data structure that the author calls a list. The author defines a list as a "nested set of objects with the first object holding a reference to the second, the second to the third, and so on."
The book includes code:
let list = {
value: 1,
rest: {
value: 2,
rest: {
value: 3,
rest: null
}When would you use this? The book says, "A nice thing about lists is that they can share parts of their structure." But if you can organize an object's structure however you like, you can choose to have them all use the same structure if you want. Does the book mean that they share content?
Even if that's what it means, can't you just include these subordinate objects into a master object to share data? Something like:
let masterList = {
value1: 1,
value2: 2,
value3: 3,
rest1: {},
rest2: {}
}Then rest1 and rest2 can share the values without interfering with each other or nesting.
Edit: the author expanded on this (linked) list by adding a way to iterate through the list. That seems to make it more useful:
Now I see a little better what people were saying as far as using this as an alternative to arrays. Still don't entirely understand when to use this, though. Are there certain clues that make you go, "Aha! This means I need to use a linked list"?
class List {
constructor(value, rest) {
this.value = value;
this.rest = rest;
}
get length() {
return 1 + (this.rest ? this.rest.length : 0)
}
static fromArray(array) {
let result = null;
for (let i = array.length - 1; i >= 0; i--) {
result = new this(array[i], result);
}
return result;
}
}
class ListIterator {
constructor(list) {
this.list = list;
}
next() {
if(this.list == null) {
return {done: true};
}
let value = this.list.value;
this.list = this.list.rest;
return {value, done: false};
}
}Edit 2: I found a Wikipedia article on linked lists. In fact, the version above is the simplest version. It looks like the point of linking these objects together is to make it "easy" (or computationally cheap) to include or exclude members from the list. There are a couple versions: there's a double-linked version, where you refer to the next object as well as the previous object; a circular version, where you take the last object and refer back to the first object; and there's a "sentinel" version, where you put a dummy object at the beginning and/or end to bypass certain edge-cases.