1

I have a object like this

{ 
   "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"
}

Do you know how i reset the items order.

now 2, 4, 7

should be 0, 1, 2

apsillers
  • 112,806
  • 17
  • 235
  • 239
  • 3
    Why don't you use an array? – Ram May 20 '15 at 14:38
  • Object properties do not have order. I assume you want to re-name the properties, counting up from 0, but have the property values keep their original relative key-name ordering? (So the property with the smallest name is renamed to `0`, the next-lowest is `1`, etc.) – apsillers May 20 '15 at 14:53

5 Answers5

1

Created a JSfiddle that shows you a way.

Im using a custom format function:

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

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

    object.items = items;
}

The resulted object is this:

{
    "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"
}
Robin H
  • 101
  • 4
0

How about this

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);
anonymelon
  • 11
  • 2
0

Object properties do not have order. I assume you want to re-name the properties, counting up from 0, but have the properties maintain the original relative ordering of their keys. (So the property with the smallest name is renamed to 0, the second-to-smallest is 1, etc.)

To do this, get all the property names, and sort the names numerically. Then, get all the values in the same over as their sorted property names. Finally, re-insert those property values with their new property names.

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];
}
apsillers
  • 112,806
  • 17
  • 235
  • 239
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++;
    }
});
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Object properties order is not guaranteed anyway. You should use an array instead.

Take a look at this answer

Community
  • 1
  • 1
redben
  • 5,578
  • 5
  • 47
  • 63