Javascript /下划线,将布尔数组转换为字符串数组 [英] Javascript/Underscore, turn array of booleans into array of strings

查看:107
本文介绍了Javascript /下划线,将布尔数组转换为字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图完成将布尔数组转换为字符串数组的操作(仅对设置为true的布尔值而言)。可以使用javascript或下划线。让我告诉你我的意思。



我有一个像这样的数组:

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

我想要的最终结果是:

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

值得一提的是,所有这些键都是动态的。我似乎无法确定如何遍历此数组以完成此任务。越简单越好!谢谢!



这是我可怜的尝试:

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

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

if(value === true){
返回值;
}
});

});

(这不起作用)。我要完成的工作是将这些对象的值(例如[[{ one:true, two:false}])转换为字符串数组,这些字符串是设置为的项目的键



例如

  [{ one: true, two:false}] 

将变成

  [一个] 

因为

解决方案

使用 lodash



1)选择值真实的对象的属性:

  _。pick(object,Boolean)

2)抓取对象属性的键其值是真实的:(与上面的解决方案结合)

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

3)对每个项目执行上述操作与 _。map值 (就像在对象上执行 Array.prototype.map 一样)

  _。mapValues(项目,函数(值){
return _.keys(_。pick(value [0],Boolean));
});

  var arr = [{ item1:[{ one: true, two:false}]},{ item2:[{ one:false, two:true}]}]];函数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>') 

 <脚本src = https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.6.0/lodash.min.js>< / script>  



更新:使用下划线



幸运的是,大多数方法都受到下划线的支持,我要做的唯一更改就是将 _。mapValues 更改为 _。mapObject



  var arr = [{ item1:[{ one:true, two:false}]},{ item2:[{ one:false, two:true}]}];函数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>') 

< pre class = snippet-code-html lang-html prettyprint-override> < script src = https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3 /underscore.js\"></script>


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.

解决方案

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>

这篇关于Javascript /下划线,将布尔数组转换为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆