Skip to content Skip to sidebar Skip to footer

Javascript/underscore, Turn Array Of Booleans Into Array Of Strings

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

Solution 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}]}];

functionrun () {
  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>')
<scriptsrc="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}]}];

functionrun () {
  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>')
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

Solution 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

Post a Comment for "Javascript/underscore, Turn Array Of Booleans Into Array Of Strings"