1

I am trying to search trhough an object to check for validation like so

var selLess = $scope.accountTreeHere.map( function(obj){
        return obj.selected;
    });

So this works fine however, there is actually one level deeper that has items with .selected that i would like to combine into this variable, this map only seems to go on the first level of the accountTreeHEre, when I need it to go down into any level looking for the key of selected. is this possible with underscore? I am new to underscore so I'm still unsure of how to do this.

I basically want to map over the entire object for anything with the key of selected, no matter how deep, and return an array of their values.

Thanks for reading!

So the accountTreeHere would look somehting like this

[{name : name 1, selected: true, subs : [{selected : true, name : sub 1},{selected : false, name : sub 2}]},{name : name 2, selected: false, subs : [{selected : false, name : sub 1},{selected : false, name : sub 2}]}

so it just has nested objects inside sub here for example that also have keys of selected that I want to add into this array.

6
  • It's a little unclear to me what you're asking here. Could you give an example of what accountTreeHere might look like? Commented Dec 19, 2014 at 20:34
  • @AlexisKing sure! one sec. Commented Dec 19, 2014 at 20:35
  • So you want the result to simply be a flat array of all the elements with selected: true? Commented Dec 19, 2014 at 20:39
  • Just a flat aray of the elements with selected, so true/false doesn't matter, right now it is just an array of like ['false',true',false], but its just for the first level down, I need that combined with the second level down too. Does that make sense? Commented Dec 19, 2014 at 20:41
  • Ah, I think I see, so the correct output for the example data would be [true, true, false, false, false, false]? Commented Dec 19, 2014 at 20:42

1 Answer 1

2

You can use a combination of _.map and _.flatten to do this without too much hassle.

var data = [
  { name: "name 1", selected: true, subs:
   [
     { selected: true,  name: "sub 1" },
     { selected: false, name: "sub 2" }
   ]},
  { name: "name 2", selected: false, subs:
   [
     { selected: false, name: "sub 1" },
     { selected: false, name: "sub 2" }
   ]}
  ];

function mapSelections(data) {
  return _.map(data, function (el) {
    return [el.selected, mapSelections(el.subs || [])];
  });
}

var selLess = _.flatten(mapSelections(data));

console.log(selLess);
document.write(JSON.stringify(selLess));
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>


As requested in the comments, here's an explanation of how this line works:

return [el.selected, mapSelections(el.subs || [])];

This is mostly just abusing how the function works to make it more concise. Since we're flattening everything at the end, anyway, we can just return an array of the value itself (el.selected) and the recursively-determined remaining selections.

We then call mapSelections on the sub-items to find the child values. The problem is that this would fail if an element doesn't have child items (if the item doesn't have a subs property). Therefore, we use JavaScript's logical or operator (||) to provide a default value if el.subs is undefined. This works because the or operator always returns the second value of the expression if the first is false.

Sign up to request clarification or add additional context in comments.

4 Comments

This is great, my question is is this possible with undescore?
@ajmajmajma Yes, both _.map and _.flatten are available in underscore.
Hey, just curious, could you explain the return [el.selected, mapSelections(el.subs || [])]; part briefly? Regardless, thanks again!
@ajmajmajma Sure, added an explanation at the end of the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.