1

I need to clarify some terminology, because right now my project has a bit of a mix of terminology when refering to variables.

Consider an object defined like this:

var anObject = {
    a: {
        value1: 1337,
        value2: 69,
        value3: "420 man"
    }
}

Please correct me if I'm wrong, but I assume a is a property of the object anObject.

But in the context of anObject, how should I refer to value1? Is it a "property" aswell?

The reason I'm asking is I need to create functions to access "variables within variables" of objects. Like such:

function getProperty(name) {
    // ...
}

var theValueImLookingFor = getProperty("a.value1");

If this questions is inappropriate for Stack Overflow, please let me know where I should as this question instead. Thanks

EDIT: I'm not asking how to access the nested variable, I'm asking how to refer to it in terminology.

Given that value1 is a property of a which is a property of anObject. What is value1 to anObject? Is it a "property-property"? I don't mean to sound flippant but what do I call it?

birgersp
  • 3,909
  • 8
  • 39
  • 79
  • Sometimes that's called *Object Graph Navigation*. – Pointy Nov 04 '16 at 14:09
  • 1
    Note that there is no JSON in here at all. JSON is a textual data format. What you have there are nothing more than JavaScript objects. – Cerbrus Nov 04 '16 at 14:11
  • `anObject.a.value1`. In function you need to split your property by `.` chars, and then you can refer like `anObject['a']['value1'];` – vaso123 Nov 04 '16 at 14:11
  • 1
    Objects have properties, and those properties can be objects, which have properties, which can be objects, which ... etc. – trincot Nov 04 '16 at 14:11
  • I know this is a duplicate, and that there's a question with solid solutions for simple dotted-name graph traversal, but I can't find it. – Pointy Nov 04 '16 at 14:12
  • @Pointy: That one should do, not? – Cerbrus Nov 04 '16 at 14:13
  • 1
    @Cerbrus yes that's the one I was thinking about. Thanks. (My view of the question hadn't updated when I typed that comment.) – Pointy Nov 04 '16 at 14:15
  • @Cerbrus please see my edit, I'm not asking how to access the variable. I'm asking how to refer to it "vocally" or in terminology. – birgersp Nov 04 '16 at 14:23
  • 1
    @Birger I know of no "standard" way of referring to a path through an object graph. In discrete math it's sometimes called a "Gorn address", but I suspect few people would understand that. – Pointy Nov 04 '16 at 14:25
  • 1
    (Note: [not this kind of "gorn".](https://en.wikipedia.org/wiki/Gorn#/media/File:StarTrek-Gorn.jpg)) – Pointy Nov 04 '16 at 14:27
  • Haha, okay I see. In your opinion, am I wrong to call `value1` a property of `anObject`? Or `a.value1` a "property" of `anObject`? – birgersp Nov 04 '16 at 14:27
  • @Birger well if I felt the need to use some term, I'd say that it's part of the object graph. However, I don't think I'd have much confidence that anybody would understand me :) I can't honestly say I recall the last time I felt the need to construct a sentence around the concept. – Pointy Nov 04 '16 at 18:02
  • 1
    @Cerbrus: IMHO it's wrong that this question is marked as duplicate, since the other question is about how to implement it, not about terminology. – Golo Roden Dec 15 '16 at 08:21

1 Answers1

2

In the same way that a is a property of anObject, value1 is a property of a.

So you can access it by using:

anObject.a.value1

That's it. The long answer is, that this can be decomposed into the following code:

const innerObject = anObject.a;
const result = innerObject.value1;

Please note that in JavaScript an object is nothing but a list of key-value pairs, i.e. a dictionary. You can use any data type as value, even another object, so you end up with nested objects - and this is exactly what you have here.

So for the function you are talking about, all you need to do is to split the given string by its separator char, such as a ., and then recursively walk the object tree until you get where you want to.

Please note that e.g. on npm there are already ready-made modules that do exactly this, e.g. nested-keys (just to name one arbitrarily, there are lots of other modules that do the same or at least a similar task).

Even very common used libraries such as Lodash provide this functionality, so there is a good chance that you do not need to code it for yourself anyway, but can get away with using such a ready-made function. E.g., you might use [get](https://lodash.com/docs/4.16.6#get] of Lodash:

var object = { 'a': [{ 'b': { 'c': 3 } }] };

_.get(object, 'a[0].b.c');
// => 3

_.get(object, ['a', '0', 'b', 'c']);
// => 3

_.get(object, 'a.b.c', 'default');
// => 'default'

(This sample code is taken from the linked Lodash documentation on get.)

If you still want to create your very own implementation, having a look at Lodash's source code may be a good starting point.

UPDATE

Regarding your edit, I don't think that there is an official term for this. You could call it a nested property, or a property of a sub-object. I think I'd rather describe as you did originally: value1 is a property of an object that is a property of anObject. That's not elegant, but technically correct, and as I said, I don't think there is a better (official) term.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • I think the OP wants to know how, given a string representation of the "path" to a leaf value in the object graph, can the value be retrieved. Your answer works when the property names are fixed, but that's not always the case. – Pointy Nov 04 '16 at 14:13
  • Right. I updated my answer to better suit the question. – Golo Roden Nov 04 '16 at 14:15
  • Please see my edit, I'm not asking how to access the variable. I'm asking how to refer to it, or what to "call it" if I were to describe it... – birgersp Nov 04 '16 at 14:24
  • @GoloRoden you answer is good but you're missing the point of my question. I know perfectly well how to access the variable within the variable. But I don't know what to call it. Hence the `(Terminology)` mark in my question which seems completely ignored... – birgersp Nov 04 '16 at 14:32
  • 1
    @Birger Thanks for clarifying this! And I'm sorry that I got your question wrong in the first place. I now added a small update. – Golo Roden Nov 04 '16 at 16:44