0

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.

  • Are you sure you want non numeric keys in your array `["jen":{`? – Yury Tarabanko Apr 03 '17 at 14:09
  • Create a recursive function that takes an object and a (key) name. Call recursively with the "children" object and it's keys. Your result should probably be an object, not an array, as others have already stated. – Mike Scotty Apr 03 '17 at 14:19
  • There are not associative arrays in javascript, just indexed arrays. You can read more about it in this other stackoverflow question: [javascript array associative AND indexed?](http://stackoverflow.com/questions/1076658/javascript-array-associative-and-indexed#1076669). – Eloy Pineda Apr 03 '17 at 14:19

3 Answers3

1

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).

Community
  • 1
  • 1
Stuart
  • 9,597
  • 1
  • 21
  • 30
  • Thanks this was the thing I was looking for! Didn't know i wanted to flatten a nested object. The jsfiddle looks prommising! Trying this now :) – UNTITLED.PNG Apr 03 '17 at 14:44
  • So I made a mistake by creating my example object. Can you please adjust your code to this array: [link](https://jsfiddle.net/emcqvmt9/) I tried to do it but without any luck.. – UNTITLED.PNG Apr 03 '17 at 15:09
0

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))
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
-3

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