-2

Consider following objecT:

JSON

{
  "id1": {
    "key1": "valueX",
    "key2": "dont care"
  },
  "id2": {
    "key1": "valueY",
    "key2": "dont care"
  },
  "id3": {
    "key1": "valueZ",
    "key2": "dont care"
  }
}

Given the parent key (e.g. "id2") is there a way to return the nested "valueY" constrained to be coming from "key1"?

To be clear:

  • Input: "id2"
  • Output: "valueY" (looking for "key1" within "id2")
Kalaschnik
  • 769
  • 1
  • 7
  • 21
  • 2
    `obj['id2']['key1']`? – Eddie Jun 27 '18 at 09:56
  • 1
    Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) –  Jun 27 '18 at 09:58
  • 1
    Imagine if there *weren't* a way. JSON would be utterly useless. –  Jun 27 '18 at 09:59
  • @Eddie OMG, this is so trivial I feel stupid. Thanks. – Kalaschnik Jun 27 '18 at 10:09
  • @ChrisG Based on your link I could not answer my question, there I don’t see this as a duplicate question, since I got a "nested" problem. But yes you were right with the wording "a way" : ) – Kalaschnik Jun 27 '18 at 10:09
  • @McTell The information you need is how to use dot / bracket notation. This is explained in the very first paragraph of the accepted answer. I interpret your question as "how do I get from a parameter `x` to `json[x].key1`, so I completely fail to see how this *isn't* a duplicate. –  Jun 27 '18 at 13:03

3 Answers3

1

You are trying to do something like this perhaps:

let json = {
  "id1": {
    "key1": "valueX",
    "key2": "dont care"
  },
  "id2": {
    "key1": "valueY",
    "key2": "dont care"
  },
  "id3": {
    "key1": "valueZ",
    "key2": "dont care"
  }
}

valueFinder = (id, key) => json[id][key]

console.log(valueFinder("id2", "key1"))
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
0

You can get in this way:

 var data={"id1": { "key1": "valueX","key2": "dont care"},"id2": {"key1": "valueY","key2": "dont care" },"id3": { "key1": "valueZ", "key2": "dont care" }}, key="id2";
console.log(key + " = " + data[key].key1);
       
NullPointer
  • 7,094
  • 5
  • 27
  • 41
0

If you know that always the name of the key you want is "key1" as shown in the example, you can use the following function:

var obj = {
  "id1": {
    "key1": "valueX",
    "key2": "dont care"
  },
  "id2": {
    "key1": "valueY",
    "key2": "dont care"
  },
  "id3": {
    "key1": "valueZ",
    "key2": "dont care"
  }
};

var getFirstChild = function(obj, key) {
  return obj[key].key1;
}

alert(getFirstChild(obj, 'id2'));

First you get the object that has the specified key, and then you search for the pair with the key1 key.

If you want to get the first key of the object, regardless of the name you can use the following:

var obj = {
      "id1": {
        "key1": "valueX",
        "key2": "dont care"
      },
      "id2": {
        "key1": "valueY",
        "key2": "dont care"
      },
      "id3": {
        "key1": "valueZ",
        "key2": "dont care"
      }
    };

    var getFirstChild = function(obj, key) {
      return obj[key].key1;
    }

    alert(getFirstChild(obj, 'id2'));

This will again search for the key you specify, get all the values, and return the first. Note that properties are ordered inside object depending on javascript implementation. Meaning that the first property you specify in your code, might not be the first property in the object after all.

Kalkost
  • 13
  • 1
  • 5