user4657847
user4657847

Reputation:

reset object order javascript

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

Upvotes: 1

Views: 1684

Answers (5)

redben
redben

Reputation: 5686

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

Take a look at this answer

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386634

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++;
    }
});

Upvotes: 0

apsillers
apsillers

Reputation: 115950

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];
}

Upvotes: 0

Robin H
Robin H

Reputation: 116

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

Upvotes: 1

anonymelon
anonymelon

Reputation: 11

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);

Upvotes: 0

Related Questions