Reputation: 11
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.
Upvotes: 0
Views: 42
Reputation: 362
<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>
Upvotes: 0
Reputation: 12990
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);
Upvotes: 2