0
const shortCode = {
  DA: ['934', '986', '879'],
  BA: ['914', '926', '849'],
  AD: ['911', '900', '899']
};

How to get the Key on that shortcode Object. Let say I have '879' value and search for the corresponding key?

In Javascript.

2

2 Answers 2

2

You can use find on Object.keys of shortCode:

const shortCode = {
  DA: ['934', '986', '879'],
  BA: ['914', '926', '849'],
  AD: ['911', '900', '899']
};

const res = Object.keys(shortCode)
                  .find(k => shortCode[k].includes('879'));
              
console.log(res);

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

Comments

0

 <script>
        const shortCode = {
            DA: ['934', '986', '879'],
            BA: ['914', '926', '849'],
            AD: ['911', '900', '899']
        };
        var f = '879';
        for (var k in shortCode) {
           
            if (typeof shortCode[k] !== 'function') {
                var arr = shortCode[k];
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i] == f) {
                        alert('key = ' + k);
                    }
                }
                
            }
        }
    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.