I am writing a function that takes an object and an array. I loop through the array and use the value as a key to look in the object.
I would like the loop to stop as soon as 1 value has been found and return that value. Somehow i keep getting undefined when running this function.
const searchInObject = function(obj, keys) {
//if array => loop and return the first value that is found.
//if not array and type is string find in object
// if no array or undefined return default value
if (Array.isArray(keys)) {
keys.map(key => {
if (obj[key]) return obj[key];
})
}
};
const obj = {
a: '1',
b: '2'
};
console.log(searchInObject(obj, ['a', 'b']));