0

I have an array of objects in which I want to get the value of a key for e.g. 'variable'. But the depth of the key is varying, like it could be as follows -

[{
    "test": {
        "nameObj": {
            "name": "DateExpires",
            "title": "DateExpires",
            "variable": "DateTime"
        },
    }
}, 
{
    "test": {
        "nameObjSomethingElse": {
            "name": "DateExpires",
            "title": "DateExpires",
            "variable": "DateTime"
        },
    }
}, 
{
    "test": {
        "nameObjSomethingElse": {
            "name": "DateExpires",
            "title": "DateExpires",
           "anotherLevel": {
            "variable": "DateTime"
           }
        }
    }
}]

In each object in the array key 'variable' is at different level and under different key. How can I get the value of 'variable'?

1
  • As I see there's more than one attribute named variable, when fetching it do you want to get them all or the first occurrence for example ? Commented Jul 25, 2019 at 10:59

1 Answer 1

6

You could use JSON.stringify and use its callback function to identify every value of a particular key:

const obj = [{
    "test": {
        "nameObj": {
            "name": "DateExpires",
            "title": "DateExpires",
            "variable": "DateTime"
        },
    }
}, 
{
    "test": {
        "nameObjSomethingElse": {
            "name": "DateExpires",
            "title": "DateExpires",
            "variable": "DateTime"
        },
    }
}, 
{
    "test": {
        "nameObjSomethingElse": {
            "name": "DateExpires",
            "title": "DateExpires",
           "anotherLevel": {
            "variable": "DateTime"
           }
        }
    }
}];
const variables = [];
JSON.stringify(obj, (key, val) => {
  if (key === 'variable') {
    variables.push(val);
  }
  return val;
});
console.log(variables);

Or, if you wanted to recurse more manually:

const obj = [{
    "test": {
        "nameObj": {
            "name": "DateExpires",
            "title": "DateExpires",
            "variable": "DateTime"
        },
    }
}, 
{
    "test": {
        "nameObjSomethingElse": {
            "name": "DateExpires",
            "title": "DateExpires",
            "variable": "DateTime"
        },
    }
}, 
{
    "test": {
        "nameObjSomethingElse": {
            "name": "DateExpires",
            "title": "DateExpires",
           "anotherLevel": {
            "variable": "DateTime"
           }
        }
    }
}];
const recurse = (obj, arr=[]) => {
  Object.entries(obj).forEach(([key, val]) => {
    if (key === 'variable') {
      arr.push(val);
    }
    if (typeof val === 'object') {
      recurse(val, arr);
    }
  });
  return arr;
};
console.log(
  recurse(obj)
);

Sign up to request clarification or add additional context in comments.

1 Comment

Unless one of the values is a function with a property 'variable', or some other object that JSON.stringify can't stringify. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.