1

So I am trying to accomplish turning an array of booleans to an array of strings, (only of the booleans that were set to true). This can be in either javascript, or underscore. Let me show you what I mean.

I have an array like this :

 [{"item1" : [{"one": true, "two": false}]}, {"item2" : [{"one": false, "two": true}]}];

And the end result I am looking for is :

[{"item1" : ["one"]}, {"item2" : ["two"]}];

It's worth mentioning, all of these keys will be dynamic. I can't seem to figuire out how I should traverse this array to complete this task. The simpler, the better! Thanks!

Here's my poor attempt :

  $scope.testObject = _.map($scope.filterArray, function(obj) {

                    _.map(obj.values, function(value) {

                        if (value === true) {
                            return value;
                        }
                    });

                });

(this does not work). What I am trying to accomplish is turning the values of these objects ([{"one":true, "two": false}] for example) into an array of strings, the strings being the keys of the items that are set to true.

So for example

 [{"one":true, "two": false}]

would turn into

  ["one"]

Because two is false.

1
  • 1) what have you tried, 2) do you mean [{"item1" : ["one"]}, {"item2" : ["two"]}] Commented Apr 11, 2015 at 3:13

2 Answers 2

5

With lodash:

1) pick the properties of an object whose value is truthy:

_.pick(object, Boolean)

2) grab the keys of the properties of an object whose value is truthy: (combining with the solution above)

_.keys(_.pick(object, Boolean))

3) do the above operation for each item with _.mapValues (which is like performing Array.prototype.map on objects)

_.mapValues(item, function (value) {
   return _.keys(_.pick(value[0], Boolean));
});

var arr = [{"item1" : [{"one":true, "two": false}]}, {"item2" : [{"one":false, "two": true}]}];

function run () {
  return _.map(arr, function (item) {
    return _.mapValues(item, function (value) {
       return _.keys(_.pick(value[0], Boolean));
    });
  })
}

document.write('<pre>' + JSON.stringify(run(), null, 2) + '</pre>')
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.6.0/lodash.min.js"></script>

update: With underscore:

Fortunately most of the methods are supported by underscore, the only change I had to do was to change _.mapValues to _.mapObject (source)

var arr = [{"item1" : [{"one":true, "two": false}]}, {"item2" : [{"one":false, "two": true}]}];

function run () {
  return _.map(arr, function (item) {
    return _.mapObject(item, function (value) {
      return _.keys(_.pick(value[0], Boolean));
    });
  })
}

document.write('<pre>' + JSON.stringify(run(), null, 2) + '</pre>')
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

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

2 Comments

This is a great answer. It's sort of a bigger project this is inside of, and it is using underscore, is this possible with underscore?
yep, I didn't know underscore had similar methods, I've updated my answer
0

This function will allow you to generate the result you need. 'For in' is useful for accessing object keys I did that twice. It may be that there is a more elegant way of implementing this feature. If you give me more information, I could try and help you?'

var arr = [{"item1" : [{"one":true, "two": false}]}, {"item2" : [{"one":false, "two": true}]}];

arr.map(function(item){
    for(key in item){
        for(innerKey in item[key][0]){
            if(item[key][0][innerKey]){
                var obj = {};
                obj[key] = [innerKey];
                return obj;
            } 
        }
    }
});

JavaScript set object key by variable

Comments

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.