Javascript/Underscore, turn array of booleans into

2019-08-07 13:45发布

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.

2条回答
爷的心禁止访问
2楼-- · 2019-08-07 13:49

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>

查看更多
混吃等死
3楼-- · 2019-08-07 14:01

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

查看更多
登录 后发表回答