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

2
  • 3
    Why don't you use an array? Commented May 20, 2015 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.) Commented May 20, 2015 at 14:53

5 Answers 5

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"
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

1 Comment

Aww man, I saw the .map(parseInt) too and thought I learned something exciting.
0

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

Take a look at this answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.