重置对象顺序javascript

时间:2015-05-20 14:36:14

标签: javascript object reset

我有一个这样的对象

{ 
   "items":{ 
      "2":{ 
         "id":122,
         "product_id":"DE",
         "price":"9.35",
      },
      "4":{ 
         "id":15,
         "product_id":"CH",
         "price":"8.00",
      }
     "7":{ 
         "id":78,
         "product_id":"CH",
         "price":"3.00",
      }
   },
   "total_price":"20.35",
   "item_count":2,
   "unit":"CHF"
}

你知道我如何重置物品订单。

现在2,4,7

应为0,1,2

5 个答案:

答案 0 :(得分:0)

这个怎么样

var obj = {
  "items":{
     "2":{
        "id":122,
        "product_id":"DE",
        "price":"9.35",
     },
     "4":{
        "id":15,
        "product_id":"CH",
        "price":"8.00",
     },
    "7":{
        "id":78,
        "product_id":"CH",
        "price":"3.00",
     }
  },
  "total_price":"20.35",
  "item_count":2,
  "unit":"CHF"
}
var keys = Object.keys(obj.items)

for (var i = 0; i < keys.length; i++) {
   obj.items[i] = obj.items[keys[i]];
   delete obj.items[keys[i]];
};

console.log(obj);

答案 1 :(得分:0)

创建了JSfiddle,向您展示了一种方式。

我正在使用自定义format功能:

function format(object) {
    var items = {};

    var i = 0;
    for (var index in object.items) {
        items[i] = object.items[index];
        i++;
    }

    object.items = items;
}

结果对象是:

{
    "items": {
        "0": {
            "id": 122,
            "product_id": "DE",
            "price": "9.35"
        },
        "1": {
            "id": 15,
            "product_id": "CH",
            "price": "8.00"
        },
        "2": {
            "id": 78,
            "product_id": "CH",
            "price": "3.00"
        }
    },
    "total_price": "20.35",
    "item_count": 2,
    "unit": "CHF"
}

答案 2 :(得分:0)

对象属性没有顺序。我假设您要重新命名属性,从0开始向上计数,但是属性保持其键的原始相对顺序。 (因此名称最小的属性重命名为0,第二个最小的属性为1等。)

要执行此操作,请获取所有属性名称,并以数字方式对名称进行排序。然后,获取与其排序的属性名称相同的所有值。最后,使用新的属性名称重新插入这些属性值。

var itemsObj = obj["items"];

// get all names
var propertyNames = Object.keys(itemsObj);

// sort property names in numeric order: ["2", "4", "7"]
propertyNames.sort(function(a,b){ return a-b; });

// get property values, sorted by their property names
// ["2", "4", "7"] becomes [{ "id":122, .. }, { "id":15, ... }, { "id":78, ... }]
var values = propertyNames.map(function(propName) { return itemsObj[propName]; }

// clear out old property and add new property
for(var i=0; i<values.length; ++i) {
    delete itemsObj[propertyNames[i]];
    itemsObj[i] = values[i];
}

答案 3 :(得分:0)

var data = {
    "items": {
        "2": {
            "id": 122,
            "product_id": "DE",
            "price": "9.35",
        },
        "4": {
            "id": 15,
            "product_id": "CH",
            "price": "8.00",
        },
        "7": {
            "id": 78,
            "product_id": "CH",
            "price": "3.00",
        }
    },
    "total_price": "20.35",
    "item_count": 2,
    "unit": "CHF"
};
var indices = Object.keys(data.items).map(function(i) { return parseInt(i, 10); }),
    counter = 0;

indices.sort();
indices.forEach(function (i) {
    if (i > counter) { // put here some more collision detecting!
        data.items[counter] = data.items[i];
        delete data.items[i];
        counter++;
    }
});

答案 4 :(得分:0)

无论如何都不保证对象属性顺序。你应该使用数组。

看看这个answer