简体   繁体   English

javascript:如何将最高层的json对象作为数组?

[英]javascript: How to get highest layer of json object as array?

With an array of objects in a form like this: 使用如下形式的对象数组:

[
    { 
        1429={
            {
                8766={...},
                8483={...},
                7345={...}
            }
        }
    },
    {
        9041={...}
    }
]

how could i get back an array like this?: [1429, 9041] 我怎么能得到这样的阵列?:[1429,9041]

If the array of objects would be in another structure this code would work: 如果对象数组将在另一个结构中,则此代码将起作用:

var obj = {
    "5": "some",
    "8": "thing"
};
var keys = $.map(obj, function (value, key) {
    return key;
});

console.log(keys);

That would return [5, 8]. 那会回归[5,8]。 But in my example it just would return the indexes [0,1] 但在我的例子中它只会返回索引[0,1]

Even if I wouldn't know the depth of the object - is it possible to get the values on that level? 即使我不知道对象的深度 - 是否有可能获得该级别的值? I dont need the indexes, I need those values. 我不需要索引,我需要这些值。 I couldn't find anything about it so far. 到目前为止我找不到任何关于它的信息。 Any tips for me maybe? 对我来说可能有什么提示?

PS: I know that i could work out something with these keys and a loop, but I'm just asking for a simplier way to do it. PS:我知道我可以使用这些键和循环来解决问题,但我只是想要一种简单的方法来实现它。

Regards 问候

you are looking for the keys in a json object, you can get them this way: 你正在寻找一个json对象中的键,你可以这样得到它们:

Object.keys(obj);

for the object example: 对于对象示例:

var obj = {
"5": "some",
"8": "thing"
};

you will get: 你会得到:

["5","8"]

for an array of object of this type: 对于此类型的对象数组:

var arrayObject = [{},{},{}]; 

you can use a map and get the keys: 你可以使用地图并获得钥匙:

var keys = arrayObject.map(function(k){
   return Object.keys(k);
});

keys is an array of arrays of keys. keys是一组键数组。 Example, for the following object (similar to your data structure): 例如,对于以下对象(类似于您的数据结构):

var l= [
{ 
    1429:{

            8766: "test",
            8483:"test",
            7345: "test"

    }
},
{
    9041: "test"
}
];

you will get: 你会得到:

[["1429"],["9041"]]

apply concat and you will get what you are looking for. 申请concat,你会得到你想要的。 Here how to apply concat in the case of multiple arrays. 这里如何在多个数组的情况下应用concat。

var arrayOfKeys = [].concat.apply([], keys);

now you will get: 现在你会得到:

["1429","9041"];

In your specific case you could use 在您的具体情况下,您可以使用

var keys = [];
root.forEach(function(v) { keys = keys.concat(Object.keys(v)); });

If instead you have a tree of arrays and you want the keys of all other objects instead (but not recursing into objects) then a simple recursive function would do it: 相反,如果你有一个数组树,而你想要所有其他对象的键(但不是递归到对象),那么一个简单的递归函数会这样做:

function topKeys(x) {
   if (x && x.constructor === Array) {
       var result = [];
       x.forEach(function(item) {
           result = result.concat(topKeys(item));
       });
       return result;
   } else if (typeof x === "object") {
       return Object.keys(x);
   } else {
       return [];
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM