Reputation: 235
I'm working on creating an online guide. It will survey users, and based on their answers, output a list that best suits their needs.
I'm about to assemble a large list of objects and properties, but before I spend too much time on it, I'd like to address some concerns:
To give you an idea of what I'm going for, here's an example using an interactive guide to finding a travel destination:
Questions for the user:
<form name="equator" action="">
<p>What side of the equator do you want to visit?</p>
<input type="radio" name="equator" value="north">North<br />
<input type="radio" name="equator" value="south">South<br />
</form>
<br />
<form name="english" action="">
<p>Does English need to be the first language?</p>
<input type="radio" name="english" value="yes">Yes<br />
<input type="radio" name="english" value="no">No<br />
</form>
<br />
<form name="beach" action="">
<p>Do you want a nearby beach?</p>
<input type="radio" name="beach" value="yes">Yes<br />
<input type="radio" name="beach" value="maybe">Maybe<br />
<input type="radio" name="beach" value="no">No<br />
</form>
The following is the way I plan to create the object list:
destinations = [
{
name: "Moscow", equator: "north", english: "no", beach: "no"
},
{
name: "Lima", equator: "south", english: "no", beach: "no"
},
{
name: "Nantucket", equator: "north", english: "yes", beach: "yes"
},
]
In the project I'm working on, the object list will actually number in the hundreds, with up-to two dozen properties. That said, here are my concerns (in case it affects your answers, I should note that my experience with Javascript only dates back about two weeks):
Thanks for your time.
Upvotes: 3
Views: 3128
Reputation: 66334
Arrays and Objects are good at storing data like this. In JavaScript, an Array is just a specific type of Object.
What you should think about is how can you make the data as "basic" as possible; i.e. if an answer has only two answers, consider using the Boolean true
or false
. If it is more, then the next stop should be integers 0
, 1
, .. In some cases in JavaScript integers work more efficiently than Boolean values. The idea I'm trying to convey is it's normally more efficient to only use a string when you actually need a string, e.g. for a name.
One of the best resources to learn JavaScript is probably Codecademy, w3schools is not a good resource.
Upvotes: 3
Reputation: 1579
Like Joseph Silber said in the comments, you'll want to change the types on english
and beach
to boolean, IE: true
or false.
As for storing your objects, I like plugins, in theory. Take a look at the tutorials on www.knockoutjs.com. First they'll show you how to create objects with name
, equator
, english
, and beach
properties. It will also show you a lot of cool things you can do with that data that'll improve user experience.
Edit: Sorry, to directly answer your question, look into JSON. You're on the right track already, I'm just not sure how you're generating your array.
Upvotes: 1