Javascript/Underscore, turn array of booleans into array of strings

孤街浪徒 提交于 2019-12-11 08:27:21

问题


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:


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>



回答2:


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



来源:https://stackoverflow.com/questions/29573598/javascript-underscore-turn-array-of-booleans-into-array-of-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!