0

Say I have this json:

"myObject" : {
  "trans": {
    "somekey": {
      "name": "i want this value"
    }
  }
}

How can I access "i want this value" without knowing the name of "somekey"?

After reading this, I understood it can be done with indexing:

myObject.trans[0].name

But I am getting an undefind. Why is that?

jamesnet214
  • 1,044
  • 13
  • 21
  • Is there always a single key in the "trans" object? – georg May 30 '21 at 09:05
  • `myObject.trans[0]` means you're trying to get the first item in an array but `myObject.trans` is an object. You cannot get the "first" key of an object as that is a meaningless term in that context. – VLAZ May 30 '21 at 09:07
  • because `trans` is not an array but an object. Objects don't have indexes they have keys. – derpirscher May 30 '21 at 09:07
  • @derpirscher Ok, so say I don't know the name of "someKey" but still want the value of "name" and I know it is the first one, what can I do? –  May 30 '21 at 09:10
  • Hint: `{ x: {y : ...} }` translates to `x.y` while `{ x: [ { y: ... } ] }` translates to `x[0].y`. Note `{` -> `.` and `[` -> `[n]`. You can also do `x['y']` instead of `x.y` but thats' because objects allow that kind of notation as well. – tadman May 30 '21 at 09:15
  • How do you define "first one"? Because per definition object-keys don't have a ordering. Can there be more than one key in `trans`? Can you modify this datamodel? If yes, I'd suggest to make `trans` an array instead of an object. – derpirscher May 30 '21 at 09:18

3 Answers3

1

Use Array if you are not sure about the key. So that you can use .find() method to find the element without knowing the index or key.

var myObject = {
  "trans": [{
    "name": "i want this value"
  }]
}

console.log(
  myObject.trans.find(item => item.name === "i want this value").name
)
Sreejith N
  • 41
  • 1
  • 8
  • Hm, as I understand the question, the value `"i want this value"` is not yet known. So how can you use it in the search? – derpirscher May 30 '21 at 09:31
  • ***How can I access "i want this value" without knowing the name of "somekey"*** @derpirscher you asked how to access without knowing "somekey". Since you never mentioned that "i want this value" is not known, I suggested an Array approach. – Sreejith N May 30 '21 at 09:35
  • First of all, I'm not OP. And second: Why would someone search for a value in an array, if he already knows that value? It seems quite obvious, that OP want to get the (yet unknown) value of `myobject.trans.somekey.name` without knowing the key `somekey` – derpirscher May 30 '21 at 09:49
1

You can use Object.keys which returns a list of an objects keys. You can then use this array of keys to select the name key from the parent object.

const myObject = {
      "trans" : {
        "somekey" : {
          "name" : "i want this value"
        }
      }
    };

const transKeys = Object.keys(myObject.trans);
const nameValue = myObject.trans[transKeys[0]].name;

console.log(nameValue);

Note - this will only work if you are sure that trans will only have one child, and that child will always have the property name. If trans contains multiple properties which may or may not contain name, you can use the array of keys to iterate through each of the properties.

petarkolaric
  • 385
  • 3
  • 16
0

If you can't change the structure of how your object looks, this is what I would suggest:

let myObject =   {
    "trans": {
        "somekey": {
            "name": "i want this value"
      }
    }
}

let finalValue;

Object.values(myObject.trans).forEach(val=>{
    if(val.name){
        finalValue = val.name;
    }
})

console.log(finalValue)