I have to find inside a json array some value of a given key, and return the value of another key of the found object.
I have made this function:
const findValueByKey = ({
jsonArray,
keyToSearch,
valueToSearch,
keyOfTheValueToReturn,
defaultMessage
}) => {
const foundObject = jsonArray.find((obj) => obj[keyToSearch] === valueToSearch);
if (foundObject) return foundObject[keyOfTheValueToReturn];
return defaultMessage || 'not found';
};
But my question is if there’s something similar with Lodash
, I don’t want to reinventing the wheel.
I know that I can find
an object given some criteria, like:
_.find(jsonArray, (object) => object[keyToSearch] === valueToSearch);
But then I should extract the key that I need, etc.