Answer the question
In order to leave comments, you need to log in
How to find recursively an object or array by key and value?
/**
* @param {array|Object}searchInArray
* @param {string|int} searchKey
* @param {string|int} searchValue
*
* @todo - use lodash?
*
* @return {Object}
*/
function walkRecursive ({searchInArray, searchKey, searchValue}) {
let returnValue = {};
if (false === Array.isArray(searchInArray)) {
searchInArray = Object.keys(searchInArray).map(function (key) {
let keyValArr = [];
return keyValArr[key] = searchInArray[key];
});
}
searchInArray.some(function checkArray (arrayItem) {
if (searchValue === arrayItem[searchKey]) {
returnValue = arrayItem;
return true;
}
return Array.isArray(arrayItem.children) && arrayItem.children.some(checkArray);
});
return returnValue;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question