Note that there are some explanatory texts on larger screens.

plurals
  1. POCan JQuery $.extend be used with indexing?
    primarykey
    data
    text
    <p>I am trying to extend an Object of the following structure (with JQuery in Coffeescript): </p> <pre class="lang-coffee prettyprint-override"><code>objectThatIsAList = { 'List Item' : callback :=&gt; return "Some value" 'Another item' : callback :=&gt; return "Another value" 'The final item' : callback :=&gt; no } </code></pre> <p>This Object is used as a (dynamic) model for a dropdown menu in a large coffeescript-based framework. </p> <p>I am extending it with </p> <pre class="lang-coffee prettyprint-override"><code>objectThatIsAList = $.extend {}, objectThatIsAList, { 'New Item' : callback :=&gt; return "New stuff" } </code></pre> <p>This results in an Object like this (comments added to show intended outcome)</p> <pre class="lang-coffee prettyprint-override"><code>objectThatIsAList = { 'List Item' : callback :=&gt; return "Some value" 'Another item' : callback :=&gt; return "Another value" # this should be last 'The final item' : callback :=&gt; no # this should not be last 'New Item' : callback :=&gt; return "New stuff" } </code></pre> <p>Obviously, the object gets extended correctly, however the order of properties is relevant for the purpose of building a list. <strong>I'd much rather want the <code>'Final Item'</code> as the fourth and not the third property</strong> of the extended Object. </p> <p>But my research did not show indexing support for the <code>$.extend</code> method. Has anyone successfully implemented such a feature and/or has tips how to go about 'inserting' object properties into another object at a certain position?</p> <p>Or is there a clever way to reposition that final item after the <code>$.extend</code>?</p> <p>Does not necessarily have to be Coffee, even JS or Pseudocode would be of great help. </p> <p><strong>EDIT:</strong></p> <p>This is the solution that i have come up with that extends the legacy code to take arrays and convert them to objects (that are being used extensively all over the framework and where changing the original code would break alot of working code):</p> <pre class="lang-coffee prettyprint-override"><code># this comparison yields a data object that is passed to a method rendering a menu if o.menu instanceof Array menuArrayToObj = {} for menuObject in o.menu for menuObjectProperty,menuObjectValue of menuObject menuArrayToObj[menuObjectProperty]=menuObjectValue if menuObjectProperty? and menuObjectValue? o.menu = menuArrayToObj else o.menu </code></pre> <p>With this in place, it can either be fed an object or a array. The latter is easily inserted into with splice. </p>
    singulars
    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.
    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