The structure you're using should be more like this: var data = {title:'Row Title 1', contents: {leftCol:'Some text for left column',rightCol:'Some text for right column'}, {leftCol:'Some text for left column',rightCol:'Some text for right column'}, {leftCol:'Some text for left column',rightCol:'Some text for right column'} , // ... That way, each row is an object with a "title" attribute and a "contents" attribute. Your loop would then look like this: for (var i=0, j=data. Length; I '+datai.
Title+''); } for(var p=0, plen=datai.contents. Length; p '+datai.contentsp. LeftCol+''); document.
Write(''+datai.contentsp. RightCol+''); } }.
The structure you're using should be more like this: var data = {title:'Row Title 1', contents: {leftCol:'Some text for left column',rightCol:'Some text for right column'}, {leftCol:'Some text for left column',rightCol:'Some text for right column'}, {leftCol:'Some text for left column',rightCol:'Some text for right column'} , // ... ; That way, each row is an object with a "title" attribute and a "contents" attribute. Your loop would then look like this: for (var i=0, j=data. Length; I '+datai.
Title+''); } for(var p=0, plen=datai.contents. Length; p '+datai.contentsp. LeftCol+''); document.
Write(''+datai.contentsp. RightCol+''); } }.
The structure of the data really doesn't matter as long as the algorithm you use to retrieve it does so properly. Then again, it's always a good practice to structure your data logically. So +1!
;) – Aadit M Shah Oct 7 at 16:32 Yes, what you wrote is true. If it were necessary that the structure be as in the OP, it would be possible to deal with that in JavaScript, but personally I like fixing problems as closely as possible to where the problem starts :-) – Pointy Oct 7 at 20:48.
If you want to make your code more robust follow these guidelines: It's always better to initialize for loops like so if you have a length: for (var I = 0, l = length; l--; i++). The reason for this syntax is explained in fuller detail by Nicholas C. Zakas.
Always store variables accessed multiple times in a local variable. It speeds up execution (e.g. Idata = datai;). Avoid duck typing as far as possible (e.g. Datai.
Title! = null). Check for the type of the variable first.
It's slower, but the code is easier to understand and maintain. Try the typeOf function at the bottom of the post (e.g. TypeOf(idata) === "Object"). It's usually always better to use === instead of == and!
== instead of! = because they don't perform type coercion which might lead to unexpected results. Instead of creating multiple inline styles, create a single class .
GreyBackground { background-color: #EEEEEE; } and set the className of each leftCol paragraph to greyBackground. Avoid using document.write. It's slow, causes reflow of the document, and halts loading assets while the page is downloading.
The best way to create dynamic content using JavaScript is to use the document. CreateDocumentFragment method as I'll explain below. It's always better to create nodes in JavaScript yourself.
If you use a string in document. Write or element. InnerHTML then the browser parses the string and converts it into the nodes anyway.
Thus using that method is slower. This is how I would have written your JavaScript: var data = "Row Title 1", { "leftCol": "Some text for left column", "rightCol": "Some text for right column" }, { "leftCol": "Some text for left column", "rightCol": "Some text for right column" }, { "leftCol": "Some text for left column", "rightCol": "Some text for right column" }, "Row Title 2", { "leftCol": "Some text for left column", "rightCol": "Some text for right column" }, { "leftCol": "Some text for left column", "rightCol": "Some text for right column" }, { "leftCol": "Some text for left column", "rightCol": "Some text for right column" } ; function typeOf(value) { if (value === null) { return "null"; } else if (typeof value === "undefined") { return "undefined"; } else { return Object.prototype.toString. Call(value).
Slice(8, -1); } } var element; var fragment = document. CreateDocumentFragment(); var idata; for (var I = 0, l = data. Length; l--; i++) { idata = datai; if (typeOf(idata) === "Object") { element = document.
CreateElement("p"); element. ClassName = "greyBackground"; element. AppendChild(document.
CreateTextNode(idata. LeftCol)); fragment. AppendChild(element); element = document.
CreateElement("p"); element. AppendChild(document. CreateTextNode(idata.
RightCol)); fragment. AppendChild(element); } else { element = document. CreateElement("b"); element.
AppendChild(document. CreateTextNode(idata)); fragment. AppendChild(element); element = document.
CreateElement("br"); fragment. AppendChild(element); } } document.body. AppendChild(fragment); Test my page and yours.In all probability mine will execute faster.
If you have any doubts feel free to ask me. Cheers! =).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.