wtf_wikipedia
wtf_wikipedia copied to clipboard
preserve order of text containing list items
This may related to fact that objects in Javascript do not preserve the order.
obj = {
a:4545,
b:"test",
c:4.5
};
With the iteration over the variables of the object obj
you would expect the following result:
4545
test
4.5
and you create the output abover with the following code
for (var variable in obj) {
if (obj.hasOwnProperty(variable)) {
console.log(obj.variable)
}
}
But order of variable/keys of the object is not deterministic in all browsers. You could also get in another browser C the following result:
test
4.5
4545
This problem could lead to additional workload to preserve the variable order of an object. E.g. with an array of variable names/keys:
obj = {
a:4545,
b:"test",
c:4.5
};
myorder = ["a","b","c"]
for (var i = 0; i < myorder.length; i++) {
console.log( obj[myorder[i]] )
}
The array preserves the order of variables/keys of the object browser independently.
Hey Niebert. Object-key order should be predictable in modern browsers, but still this is good to keep in mind.
Using a customized wtf-debug instance and looking at the console.log() of wtf list objects of this example, could we not just add an increasing order number (which resets for each section, or even better perhaps: some global numbering scheme like eg. <section-nr>, <section-item-nr>, <sentence-item-nr>
) to all major wtf objects (sentences, lists, templates, etc)? A <sentence-item-nr>
would only increment for items within a sentence. Not sure how tables with complex content would work with this though, perhaps tables should only be ordered to the <section-item-nr>
.
This way we would get the natural order of things, and would be able to render them in that order if so desired. For some elements a different presentation order may be desirable and that could still work, if the user ignores/removes that element (or those types of elements) from the ordered list and places it somewhere else.
If this could work, it would solve a lot of inline-content issues. Eg. to show the "quote" at the right position in the section or the list. A first step to tackle this could be by just adding this "3-level" ordering data to all objects and later reviewing it in the debugger by parsing the objects in order.