简体   繁体   English

如何将Javascript OBJECT / JSON中的所有项目分离到数组中

[英]How to seperate all items in a Javascript OBJECT / JSON to a array

Ok this is the Javascript object / JSON: 好的,这是Javascript对象/ JSON:

{
    "marco":{
        "gender":"men",
        "age":29,
        "children":{
            "jen":{
                "gender":"women",
                "age":14
            },
            "marco jr.":{
                "gender":"men",
                "age":16,
                "children":{
                    "Koos":{
                        "gender":"men",
                        "age":1
                    }
                }
            }
        }
    },
    "lisa":{
        "gender":"women",
        "age":20
    },
    "mike":{
        "gender":"men",
        "age":32,
        "children":{
            "carl":{
                "gender":"women",
                "age":19
            }
        }
    }
}

I want that if I select marco that the output will be 我希望如果我选择marco ,输出将是

["jen":{"gender":"women", "age":14}, "marco jr.":{"gender":"men", "age":16}, "Koos":{"gender":"men", "age":1}]

In words: an big object with multiple layers needs to be sliced down and in 1 big array. 换句话说:需要将多层的大对象切成一个大数组。 So there is no three of objects anymore. 因此,不再有三个对象。

I can't think of any solution. 我想不出任何解决方案。

This is a suitable task for a recursive function because you need to find all the children of Marco, all of their children, and so on. 这是递归函数的合适任务,因为您需要找到Marco的所有子代,所有子代,依此类推。 Something like this: ( JSFiddle ) 像这样的东西:( JSFiddle

function getDescendents(family, memberName) {
    function inner(children, r) {
        for (var k in children) {
            if (children.hasOwnProperty(k)) {
                r[k] = {
                    age: children[k].age,
                    gender: children[k].gender
                };
                inner(children[k].children, r);
            }
        }
    }

    var r = {};
    inner(family[memberName].children, r);
    return r;
}
console.log(getDescendents(family, 'marco'));

This is essentially flattening a nested object - so also see previous answers such as Fastest way to flatten / un-flatten nested JSON objects ; 这本质上是扁平化嵌套对象-因此也请参见先前的答案,如扁平化/扁平化嵌套JSON对象的快速方法 ; One liner to flatten nested object ; 一根衬垫压平嵌套的物体 ; Flattening deeply nested array of objects . 展平深层嵌套的对象数组

This is assuming that you want to return an object with names as keys, rather than an array (which can only have index numbers). 假设您要返回一个以名称作为键的对象,而不是一个数组(只能有索引号)。

You can use recursion to do this. 您可以使用递归来做到这一点。 When you match the name of parent you need to add true to next iteration of recursion so that it knows to add to result. 当您匹配父级的名称时,您需要在下一个递归迭代中添加true,以便它知道要添加到结果中。

 var obj = {"marco":{"gender":"men","age":29,"children":{"jen":{"gender":"women","age":14},"marco jr.":{"gender":"men","age":16,"children":{"Koos":{"gender":"men","age":1}}}}},"lisa":{"gender":"women","age":20},"mike":{"gender":"men","age":32,"children":{"carl":{"gender":"women","age":19}}}} function toArray(data, name, parent) { var result = []; for (var i in data) { if(i == name || parent == true) { if(i != name) result.push({[i]: {gender: data[i].gender, age: data[i].age}}) result = result.concat(toArray(data[i].children, name, true)) } result = result.concat(toArray(data[i].children, name, false)) } return result; } console.log(JSON.stringify(toArray(obj, 'marco', false), 0, 4)) console.log(JSON.stringify(toArray(obj, 'marco jr.', false), 0, 4)) console.log(JSON.stringify(toArray(obj, 'mike', false), 0, 4)) 

If you are using javascript to search the json object use " JSON.parse() " function. 如果您使用JavaScript搜索json对象,请使用“ JSON.parse() ”函数。

Look at this page https://www.w3schools.com/js/js_json_parse.asp 查看此页面https://www.w3schools.com/js/js_json_parse.asp

Or if you are using PHP use " json_decode() " method to convert this into array 或者,如果您使用的是PHP,请使用“ json_decode() ”方法将其转换为数组

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

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