14

I have the following associative array structure in JavaScript

Array (
    [-1] => Array (
        catId : -1
        [subCatId] => Array (
             subCatId : -3
            [0] => Array (
                property : value
            )
            [1] => Array (
                property : value
            )
        )
    )
    [-4] => Array (
        catId : -4
        [subCatId] => Array (
             subCatId : -6
            [0] => Array (
                property : value
            )
            [1] => Array (
                property : value
            )
        )
    )
)

I want to convert this into numeric Array, like this

Array(
    [0] => Array(
        catID : -1
        [subCatId] => Array (
             subCatId : -3
            [0] => Array (
                property : value
            )
            [1] => Array (
                property : value
            )
        )
    )
    [1] => Array(
        catID : -4
        [subCatId] => Array (
             subCatId : -3
            [0] => Array (
                property : value
            )
            [1] => Array (
                property : value
            )
        )
    )
)

I have tried using,

var numeric_array = new Array();
for (var items in Array){
    numeric_array=Array[items];
}

but its not achieving my required result, any suggestions or comments are greatly appreciated.

1 Answer 1

22

try this:

var numeric_array = new Array();
for (var items in Array){
    numeric_array.push( Array[items] );
}

btw, I'm not sure Array is good name for you entire array:

var hash_array = []; // array which you have
...
var numeric_array = [];
for ( var item in hash_array ){
    numeric_array.push( hash_array[ item ] );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Have used Array as a Reference name, my arrayName is different.Thanks for the suggestion, its achieving my required result.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.