Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript object vs array
    primarykey
    data
    text
    <p>I am working on a javascript plugin at work which will take an array of elements and display it in a customized way (sorting, order of columns, criterias) in a table. The customization options will be created dynamically (date property will produce 'before' criteria etc). </p> <p>The plugin will be used in many different projects so the elements can vary a lot and as the display is customized the properties needed for each table creation varies as well.</p> <p><strong>Should I ask for the elements as objects or as arrays?</strong></p> <p>Basically (not real code - I will not use foreach ;) what is done is this:</p> <pre><code>foreach element in allelements row=new tablerow foreach columnid in chosencolumns row.add(element[columnid]) table.add(row) </code></pre> <p>If each element is an array the columnid is the index but if each element it will be the property name.</p> <p>Is element[3] faster than element['prop3']?</p> <p>I have made a <a href="http://datareporting.99k.org/" rel="nofollow">Beta version here</a> which have the elements stored as arrays but I am not sure if that is the best/fastest way</p> <p>EDIT: Due to the nature of the project ordering of the properties of an element is not important. <strong>ALSO: which way will consume more memory? Some systems will consist of many elements with many properties so memory might be an issue :-/</strong></p> <p>SECOND EDIT: If I use objects I would also need to make an array of property names for the object. So if the user chose to have the view consist of the 13th, 2nd and 4th property as column 1, 2 and 3 I would need to do the following for each element</p> <pre><code>row.add(element[propertynames[13]]) row.add(element[propertynames[2]]) row.add(element[propertynames[4]]) </code></pre> <p>Whereas if I chose the array solution I would 'save' the propertynames lookup:</p> <pre><code>row.add(element[13]) row.add(element[2]) row.add(element[4]) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload