简体   繁体   中英

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

Ok this is the Javascript object / 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

["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. Something like this: ( 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 ; 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.

 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.

Look at this page https://www.w3schools.com/js/js_json_parse.asp

Or if you are using PHP use " json_decode() " method to convert this into array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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