There is the following object: subcats = {20: [49, 50, 51], 16: [54, 55]};

How can I find the key of the object to which the number found in its array falls?

Suppose I got the number 50 , so in subcats this value is a child of 20 . The same with 55 or 54 - should get 16 .

Tried methods find and findIndex but there only simple arrays.

    1 answer 1

    You can get a list of keys using Object.keys() and apply find or findIndex to it already:

     var subcats = {20: [49, 50, 51], 16: [54, 55]}; console.log(Object.keys(subcats).find(function(key){ return this[key].includes(50);}, subcats)); console.log(Object.keys(subcats).find(function(key){ return this[key].includes(55);}, subcats)); 

    Or go through the keys using for..in

     var subcats = { 20: [49, 50, 51], 16: [54, 55] }; function findKey(obj, key) { for (var i in obj) { if (obj[i].includes(key)) return i; } } console.log(findKey(subcats, 50)); console.log(findKey(subcats, 54));