3

I have an object inside an object, the name of the object could change, how can I get the value inside of the object's object without referring to it by name?

Object {medium-editor-1453741508068-0: Object}
    medium-editor-1453741508068-0: Object
    value: "this is what i want"

This gets the above:

this.mediumEditor.editor.serialize();

I need something like:

this.mediumEditor.editor.serialize().childObject.value;
10
  • It depends - how did you get this object? What made it and how much control do you have over it? Can you decide what kind of object it is (and potentially create your own prototype of an Object that you can use to find specific data). My mind thinks getter and setter, that might be ideal. I do have to mention that your example object is very unclear as to what you are trying to accomplish. Commented Jan 25, 2016 at 17:10
  • I can't change the object, it comes from medium editor. Commented Jan 25, 2016 at 17:11
  • Will it always be named medium-editor- ... ? Commented Jan 25, 2016 at 17:12
  • It may have any name, although medium-editor-* is likely Commented Jan 25, 2016 at 17:13
  • 1
    You can use Object.keys() to get an array of keys. Then you can do something like Object.keys(obj).forEach(function(){}). Commented Jan 25, 2016 at 17:15

1 Answer 1

4

If your top-level object only contains one object, you can simply use Object.keys(topLevelObject) to get the "object's object" name. Something like the following:

var objectsObjectName = Object.keys(topLevelObject)[0];
var value = topLevelObject[objectsObjectName].value;

The same logic can be used recursively if you have more levels of object nesting, or if the object's object's value's name (wow, this is getting hard to follow) is non-predictable too.

You can also iterate on the top-level object:

for (var key in topLevelObject) {
    var value = topLevelObject[key].value;
}

This works even if you only have one nested object, even though it's arguably weird to write a loop knowing it will only loop once.

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

5 Comments

Wowzers, that was fast! I posted, immediately saw the error and edited... and you still got me. And now you've deleted your comment and I look like I'm talking to myself.
For safety, and certainly if we ever want to use this recursively, you should use topLevelObject.hasOwnProperty(key) inside a for loop to avoid any unexpected and inherited parts getting in the way.
@somethinghere: almost certainly, yes, though not necessarily. For intance, not in the weird case where some of the nested objects are objects inheriting from others (in which case hasOwnProperty would return false). The "top-level object" here might be an internal object of some embedded text editor, whose value we are trying to pry from its guts. In that case, the editor could inherit from some other objects. You never know.
Getting new insights every day :)
JavaScript always keeps you guessing!

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.