Comment #97 on issue 164 by mendesjuan: Wrong order in Object properties interation
http://code.google.com/p/v8/issues/detail?id=164

Let me chime in also since everybody's throwing their two cents in the fire.

I wish Chrome would keep the order of iteration for properties as they were added. Throughout browser history, browsers have mimicked existing de-facto standards even if they weren't in the actual standards. Having said that, I'm glad I made a conscious decision not to rely on that 'feature' when I first noticed it specifically because of things like this.

I always use arrays when I need to rely on order. If I also need quick access to objects by a key, I run the array through the following method to create a map keyed by specific property

/**
* Given an array and a property name to key by, returns a map that is keyed by each array element's chosen property
 * This method supports nested lists
* Sample input: list = [{a: 1, b:2}, {a:5, b:7}, [{a:8, b:6}, {a:7, b:7}]]; prop = 'a' * Sample output: {'1': {a: 1, b:2}, '5': {a:5, b:7}, '8': {a:8, b:6}, '7':{a:7, b:7}}
 * @param {object[]} list of objects to be transformed into a keyed object
 * @param {string} keyByProp The name of the property to key by
 * @return {object} Map keyed by the given property's values
 */
function mapFromArray (list , keyByProp) {
  var map = {};
  for (var i=0, item; item = list[i]; i++) {
    if (item instanceof Array) {
      // Ext.apply just copies all properties from one object to another,
// you'll have to use something else. this is only required to support nested arrays.
      Ext.apply(map, mapFromArray(item, keyByProp));
    } else {
      map[item[keyByProp]] = item;
    }
  }
  return map;
};

I think the above is better than appending characters to keys from both the server and client side.

To web devs: future proof your code, don't rely on non-standard behavior.

To Chrome devs, don't be such hard a$$es about keeping an existing behavior that many are asking for.

--Juan

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to