-3

i want to get obj[key_name] (note, key_name is without quotes!):

function myfunc(name){  
   obj = {'key_name': 'hello', key_name: 'world' };
   return obj[name];
}
//myfunc("key_name");  // <--------- this returns obj["key_name"], instead of obj[key_name]
//myfunc(key_name);    // <--------- error.  key_name is undefined

alert(myfunc("key_name"));

how to handle this?

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 1
    Your function should be working as is. Can you show us a [mcve] where it is not working. – Heretic Monkey Feb 28 '17 at 21:48
  • Have you checked that key_name var is initialized when you are calling `myfunc(key_name)`? try using `console.log(key_name)` to check the value of the variable in the browser console – Paraíso Feb 28 '17 at 21:49
  • 1
    Quotation marks denote a **string**. If you remove the quotation marks then you are trying to pass the value of the **variable** `key_name` to `myfunc`. Please provide a *complete* example. The information you provided is insufficient for solving your problem. With 16k rep you should know how to ask a proper question. – Felix Kling Feb 28 '17 at 21:50
  • Why did you edit your question to make it not reproducible? The original code used was `return obj.name` – baao Feb 28 '17 at 21:53
  • @baao: In that case it would be a duplicate of [Dynamically access object property using variable](http://stackoverflow.com/q/4244896/218196) – Felix Kling Feb 28 '17 at 21:54
  • Good point @FelixKling, too bad I did vote to close as unclear what you're asking already... – baao Feb 28 '17 at 21:55
  • 1
    @baao: That's a valid close reason for this question too ;) – Felix Kling Feb 28 '17 at 21:56
  • thanks guys, i have updated question, which gives you clear image. – T.Todua Feb 28 '17 at 21:57
  • Ugh, it's not the right duplicate (I didn't realize the property name is `key_name`, not `name`). The code you have works as it is so there is no problem. – Felix Kling Feb 28 '17 at 21:59
  • @FelixKling but now it's not the variable key any more, now the question is "why can't I declare a key twice within the same object" – baao Feb 28 '17 at 21:59
  • @baao: yeah, my mistake (fixed my comment). I guess the question is still not quite clear. – Felix Kling Feb 28 '17 at 22:00
  • `'key_name': ...` and `key_name: ...` are exactly the same thing in an object literal. – Felix Kling Feb 28 '17 at 22:01
  • i cant understand why it's unclear .. maybe answer is clear for you, but not for me.. it's modified again. – T.Todua Feb 28 '17 at 22:01
  • 1
    Well it is. He is declaring a key twice, so the first gets overriden. – baao Feb 28 '17 at 22:01
  • @T.Todua: It's unclear what you think the code is doing and which outcome you expect. We can make assumptions but it's much better if you provide a proper explanation. E.g. for us it is clear that `{'key_name': 'hello', key_name: 'world' };` will create an object with a single property. I guess that is not clear to you, but how should we know? You are also using confusing notation: `obj["key_name"]` tries to access the property with name `key_name`. `obj[key_name]` tries to access the property with a name equal to the value of the *variable* `key_name`. But that's not what *you* mean with that – Felix Kling Feb 28 '17 at 22:03
  • @FelixKling ok, now its clear for me:) I thought that they were different keys.. I'll choose bao's answer. thanks guys – T.Todua Feb 28 '17 at 22:05

2 Answers2

2

You can't have the same key twice within an object. The property

'key_name' 

is exactly the same as

key_name

so the second declaration will override the first one.

DEMO (foo will have 1 property - so the keys array length will be 1):

let foo = {
    key_name: 'foo',
    "key_name": 'bar',
    'key_name': 'baz'
}

console.log(Object.keys(foo).length); // 1
console.log(foo.key_name); // baz
baao
  • 71,625
  • 17
  • 143
  • 203
0

Your code is already working:

function myfunc(name){  
  obj = {key_name:"blah"};
  return obj[name];
}

console.log(myfunc("key_name")); // shows "blah"
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122