javascript 将关联数组转换为数值数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5243161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:30:36  来源:igfitidea点击:

Convert associative array to numeric array

javascriptassociative-array

提问by Kalpesh Jain

I have the following associative array structure in JavaScript

我在 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.

但它没有达到我要求的结果,非常感谢任何建议或意见。

回答by Maxym

try this:

试试这个:

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

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

顺便说一句,我不确定Array你整个数组的好名字:

var hash_array = []; // array which you have
...
var numeric_array = [];
for ( var item in hash_array ){
    numeric_array.push( hash_array[ item ] );
}