Javascript,用下划线(或不)转换对象 [英] Javascript, Transforming object with underscore (or not)

查看:25
本文介绍了Javascript,用下划线(或不)转换对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备要发送的对象.它最初看起来像这样:

I am looking to prepare an object for send. It initially looks like so :

var myObj = {"state":{"Saved":true,"Committed":false,"Published":false},"discipline":{"Marketing":true}};

并且需要通过查看内部对象并将其转换为值为真的对象的键数组来进行更改.所以在这个例子中它会变成这样:

And needs to be change via looking at the inner objects and turning it into an array of keys of the objects who's values are true. So in this example it would change into this :

 {"state":["Saved"],"discipline":["Marketing"]};

(committed 和published 被删除,因为它们被设置为false.)

(committed and published were removed because they were set to false. )

这是我迄今为止尝试过的:

Here is what I've tried so far:

                function mutate() {
                   //map obj
                    return _.map(myObj, function(object){ 
                     //pluck keys that have = true
                        return _.keys(_.pick(object[0], Boolean));
                     });
                }

有点挣扎,以及如何正确遍历和构建它.我正在使用下划线 - 但如果更有意义的话,普通的 javascript 就可以了.谢谢!

Struggling with this a bit, and how to traverse and build this correctly. I am using underscore - but plain javascript is fine if it makes more sense. Thanks!

推荐答案

您可以在 vanilla JS 中使用 mapreduceObject.keys 解决这个问题:

You can solve this in vanilla JS using map, reduce, and Object.keys:

var myObj = {
  "state": {
    "Saved": true,
    "Committed": false,
    "Published": false
  },
  "discipline": {
    "Marketing": true
  }
};

var output = Object.keys(myObj).reduce(function(p, collection) {
  var values = myObj[collection];
  p[collection] = Object.keys(values).filter(function(key) {
    return values[key] === true;
  });
  return p;
}, {});

document.getElementById('results').textContent = JSON.stringify(output);

<pre id="results"></pre>

这是由:

  1. 获取外部对象的键并将每个键减少到该键上的 true 值集合.
  2. 在reduce中,我们:
  1. Taking the keys of the outer object and reducing each to a collection of true values on that key.
  2. In the reduce, we:
  1. 仅过滤真实值
  2. 只返回每个值的键

该算法相当简单,几乎适用于您扔给它的任何对象.您可以轻松更改过滤器,并且可以尝试一些优化.

The algorithm is fairly simple and should work with almost any object you throw at it. You can change the filter easily and there may be a few optimizations you could try out.

下划线的等价物是:

    var myObj = {
      "state": {
        "Committed": false,
        "Saved": true,
        "Published": false
      },
      "discipline": {
        "Marketing": true
      }
    };

    var output = _.reduce(myObj, function(p, values, collection) {
      p[collection] = _.chain(values).map(function(value, key) {
        console.log('filter', arguments);
        if (value === true) {
          return key;
        }
      }).filter(function(it) {
        return !!it;
      }).value();
      return p;
    }, {});

    document.getElementById('results').textContent = JSON.stringify(output);

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<pre id="results"></pre>

这篇关于Javascript,用下划线(或不)转换对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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