Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Object.inject method #57

Closed
wants to merge 1 commit into from
Closed

Add Object.inject method #57

wants to merge 1 commit into from

Conversation

trentrichardson
Copy link

I've added an Object.inject function to inject a key/value into an object at a specific location. Not sure of the approval process but thought I would share:

Object.inject(object, newKey, newVal, fn, bind)
Inserts the item into the object where the fn parameter returns true

  • object = the object to work on
  • newKey = the new key to insert
  • newVal = new value to insert
  • fn = a function called per iteration, return true to insert at this position
    function (previousKey, previousValue, nextKey, nextValue){ }
  • bind = the object to use as 'this' in the function.

So given such object:

var tmp1 = { 
    Tom: 'first', 
    Jane: 'second', 
    Sally: 'third', 
    Bill: 'forth', 
    Lydia: 'fifth', 
    Carl: 'sixth', 
    Stan: 'seventh', 
    Earl: 'eighth', 
    Paul: 'ninth', 
    Tray: 'tenth' 
};

I can insert the key "Trent": "NEW ITEM" at the position before key item "Earl":

var tmp2 = Object.inject(tmp1, 'Trent', 'NEW ITEM', function(ak, av, bk, bv){ 
        return (bk=='Earl'); 
    });

This is particularly useful when we have objects which were sorted in a particular order and must add values in the correct positions.

@ibolmo
Copy link
Member

ibolmo commented Jan 26, 2011

This is invalid since looping through each key and object may seem as if it follows the order that you defined the object, but it's not part of the EcmaScript standard.

See: http://stackoverflow.com/questions/648139/is-the-order-of-fields-in-a-javascript-object-predicatble-when-looping-through-th

There's plenty of ways get an ordered list (linked list, array of keys and array of values, and so on) and depending on your design/requirements you'll want to choose the best one that fits.

@trentrichardson
Copy link
Author

Thanks for the response. I was previously unaware of this. It seems most browsers respect this, but indeed it is not standard, and it is clear the issue with Chrome according to the response in this post:

http://stackoverflow.com/questions/280713/elements-order-for-in-loop-in-javascript

I believe I personally will continue using it as the gray area does not effect my scenario, but I you are right and so it may not belong in core.

@sebmarkbage
Copy link
Member

Ordered enumeration may well be standardized later on but it's unclear how that process will look. Until then, this is invalid.

One alternative is this map implementation that preserves enumeration order and allows Object keys. https://github.com/kamicane/mootools-table/blob/master/Source/Table.js

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants