lodash - Javascript, check if current part object is inside other part object -
i trying loop on object , check if property inside other parts of object. trying see if first level.parentsearch inside of other firstlevel.filters array (of objects).
so object looks example :
var currentfilters = [{ "id": "topics", "name": "topics", "filters": [{ "id": "issubtopic", "label": "issubtopic", "search": "issubtopic", "num": 15 }] }, { "id": "issubtopic", "name": "issubtopic", "parentsearch": "issubtopic", "filters": [{ "id": "subtopicfilter", "label": "subtopicfilter", "search": "subtopicfilter", "num": 2 }, { "id": "subtopicfilter1", "label": "subtopicfilter1", "search": "subtopicfilter1", "num": 2 }] }, { "id": "notsubtopic", "name": "notsubtopic", "parentsearch": "uniueparentsearch", "filters": [{ "id": "notsubtopic1", "label": "notsubtopic1", "search": "notsubtopic1", "num": 5 }] } ];
so trying achieve loop on object , modify little bit (if necessary) , return it. trying check first level if .parentsearch property inside of other objects .filter array .search property. in example issubtopic looking for, because inside topics filters array.
this first time trying these kind of problem, if missing please let me know. figured since want return modified object, reduce object , check inside. part struggling bit checks if current parentsearch (in reduce loop) in other objects filter array (under .search property). have lodash mess around have tried both _.find
, _.has
, think not approaching correctly. any/all input appreciated!
here trying : https://jsfiddle.net/0fttkyey/32/
function checkifsubtopic(memo, value, key) { if(value.parentsearch) { //check if value.parentsearch in of value.filters console.log("find?", _.find(currentfilters, value.parentsearch)); if(_.find(currentfilters, value.parentsearch)){ console.log("is subtopic?"); } else { console.log("not sub topic"); } } return memo; }
interestingly, currentfilters
not same in jsfiddle provide, gives no result.
if understand correctly, try achieve is:
- for each "filter" in
currentfilters
array, check ifparentsearch
member truthy. - if so, loop through other filters.
- for each of these filters, loop through
filters
array member. - if
search
member of 1 of objects infilters
array equalparentsearch
value, keep current "filter".
so have 3 nested loops.
the 2 outer loops iterate on same currentfilters
array, except 2nd (inner) 1 skips filter parentsearch
value being searched for.
as parentsearch
value found, break loops 2 , 3 (the 2 inner-most) , go next item of 1st (outer-most) loop.
without using lodash , building new result array (instead of modifying currentfilters
array in place may yield unexpected results), have example:
var = 0, j, ifiltermax = currentfilters.length, currentparentsearch, currentfiltersarray, k, result = []; (; < ifiltermax; += 1) { // loop 1. currentparentsearch = currentfilters[i].parentsearch; if (currentparentsearch) { // if `parentsearch` truthy. loop_j: (j = 0; j < ifiltermax; j += 1) { // loop 2. if (j != i) { // skip current filter `parentsearch` being searched for. currentfiltersarray = currentfilters[j].filters; (k = 0; k < currentfiltersarray.length; k += 1) { // loop 3. if (currentfiltersarray[k].search === currentparentsearch) { result.push(currentfilters[i]); // keep current "filter". console.log("found " + currentparentsearch + " (from item #" + + ") in item #" + j + ", filter #" + k); break loop_j; // break loops 2 , 3. } } console.log("did not find " + currentparentsearch + " (from item #" + + ") in other filter."); } } } } console.log(result);
updated jsfiddle: https://jsfiddle.net/0fttkyey/71/ (with currentfilters
question above, instead of 1 in jsfiddle version 32).
Comments
Post a Comment