Search code examples
javascriptfor-loopfor-in-loop

ParseInt inside for in loop


Is there a simple way to remove the key = parseInt( key ); and get the key direct as integer from the for?

for( var key in object.items )
{
    key = parseInt( key );
}

Edit for better understanding

object.items is a object with ids as keys. But not a array.

object.items = {
    "123": "somthing",
    "745": "somthing",
    "975": "somthing"
}

Solution

  • This will extract the keys for you:

    Object.keys(object.items);
    

    Now if we want all of them as integer.

    Object.keys(object.items).map(Number);