0

Following is an example of js object:

var arr =          
{
      "a1": { "0.25": 13, "0.5": 50},
      "a2": { "0.5": 50, "0.75": 113, "1": 202}
}

id = 'a1';
key = "0.25";

function myFunc(id, key) {
  return arr[id][key];
}

Using the above function I can get the value of the corresponding key.

My question how can I get the Key for a given value?

E.g. if id = 'a2' and value = 113, the function should return the corresponding key 0.75

3
  • Have a look at this answer to the same question: stackoverflow.com/questions/9907419/… Commented Mar 16, 2019 at 3:26
  • It’s important to note that this is not a multidimensional array. There are no arrays in your code. This is an object where each key is a string and the values are objects. An array would be [1,2,3]. Commented Mar 16, 2019 at 3:27
  • I agree it's not an array but its an Object. thanks for correcting and thank you all for the help. Commented Mar 16, 2019 at 3:34

3 Answers 3

1

You can use Object.entries to get the key

var arr = {
  "a1": {
    "0.25": 13,
    "0.5": 50
  },
  "a2": {
    "0.5": 50,
    "0.75": 113,
    "1": 202
  }
}

function myFunc(a, key) {
  var k = Object.entries(arr[a]).flat();
  return k[k.indexOf(key) - 1]
}
console.log(myFunc('a1', 13))

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

Comments

1

You can get keys for arr[a] first and than filter key based on value

var arr ={"a1": { "0.25": 13, "0.5": 50},"a2": { "0.5": 50, "0.75": 113, "1": 202}}

let a = 'a1',val = 13;

let getKey = (a,val) =>  Object.keys(arr[a]).filter(e => arr[a][e] === val)

console.log(getKey(a,val))
console.log(getKey('a2',113))

If you're sure that there's always one key with matching value or you want the first matching values key only than you can use find instead of filter

var arr ={"a1": { "0.25": 13, "0.5": 50},"a2": { "0.5": 50, "0.75": 113, "1": 202}}

let a = 'a1',val = 13;

let getKey = (a,val) =>  Object.keys(arr[a]).find(e => arr[a][e] === val)

console.log(getKey(a,val))
console.log(getKey('a2',113))

4 Comments

How is that an array?
@BhojendraRauniyar because i using filter in case there are more than one key which are having value than i am taking all the keys
I meant to the object to which OP is calling an array. You should state that's not an array in your answer.
@BhojendraRauniyar oops i thought were asking about the output of first snippet is in array :P OP already updated his question with proper naming
0

Use Object.entries() and Array#find

var arr ={"a1": { "0.25": 13, "0.5": 50},"a2": { "0.5": 50, "0.75": 113, "1": 202}}

var a = 'a2',val = 113;

var Key = (a,val) =>  Object.entries(arr[a]).find(i=> i[1] == val)[0];

console.log(Key(a,val))

2 Comments

On Side note:- It's worth mentioning that find will give only the first matching value's key. if there are more than one matching value's it won't return all the keys
@CodeManiac .Op not mention multiple matching

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.