为什么lodash将我的数组转换为对象? [英] Why lodash converts my array into object?

查看:148
本文介绍了为什么lodash将我的数组转换为对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是lodash的新手,并创建了一个函数,用于从值为null或空白的对象中删除键。
但是当我传递包含某个部分作为数组的对象时,它会移除数组并将其转换为对象。

I am new to lodash, and created a function that removes the key from the object where a value is null or blank. But when i am passing my object which contains some part as array it removes array and converts it into an object.

下面是我尝试的代码:

Below is my code which i tried:

     _.mixin({ 'removeFalsies': this.removeFalsies });
        _.mixin({
                removeEmptyObjects: this.removeEmptyObjects
          });

         removeFalsies (obj) {
    return _.transform(obj, function (o, v, k) {

      if (v && typeof v === 'object') {

        if (v !== '') {
          o[k] = _.removeFalsies(v);
        }

      } else if (v === false) {
        o[k] = v;
      } else if (v) {
        o[k] = v;
      }
    });
  }
  removeEmptyObjects (obj) {
    return _(obj)
      .pickBy(_.isObject)
      .mapValues(_.removeEmptyObjects)
      .omitBy(_.isEmpty)
      .assign(_.omitBy(obj, _.isObject))
      .value();
  }

下面是我提供的JSON,省略空白和空值,所以它应该从smallMap对象中删除所有属性作为系列,对齐是空白对象,也应该删除高度。

Below is the JSON which i am providing to omit blank and null value, so it should remove all the properties from "smallMap" object as series, align are blank object and should remove height as well.

另一方面,它应该删除mid 来自heatrules以及来自children数组的series1。

On the other hand, it should remove "mid" from heatrules as well as series1 from children array.

var finalProp = {
  "smallMap": {
    "series": {},
    "align": {},
    "height": ""
  },
  "series": [
    {
      "heatRules": [
        {
          "min": "#555",
          "mid": null
        }
      ],
      "mapPolygons": {
        "tooltipText": "{name}",
        "togglable": true
      },
      "id": "value"
    }
  ],
  "children": [
    {
      "type": "HeatLegend",
      "width": "100%",
      "series1": null
    }
  ]
}
_.removeEmptyObjects(_.removeFalsies(finalProp));

它正在按预期删除所有内容,但只有1个问题是将数组转换为对象。

It is removing everything as expected but only 1 issue it is converting array to object.

它提供以下不正确的输出而不是系列:{0:{}}它应该提供系列:[{}]而不是儿童:{0:{}}它应该提供[{}]。

It is providing below incorrect output instead of series: {0 : {}} it should provide series: [{}] and instead of children: {0 :{}} it should provide [{}].

{
      "series": {
        "0": {
          "heatRules": {
            "0": {
              "min": "#555"
            }
      },
      "mapPolygons": {
        "tooltipText": "{name}",
        "togglable": true
      },
      "id": "value"
    }
  },
  "children": {
    "0": {
      "type": "HeatLegend",
      "width": "100%"
    }
  }
}

我无法找到问题的位置。

i am not able to find where is the issue any help is appreciated.

推荐答案

@Akrion提到的主要两个问题是你可以做些什么g如下:

main two problems one which was mention by @Akrion for that you can do something as below:

removeFalsies (obj) {

return _.transform(obj, function (o, v, k, l) {

    // here you have to check for array also as typeof will return object for array also
  if (v && _.isObject(v) && !_.isArray(v)) {

    if (v !== '' && !_.omitBy(v, _.isObject)) {
      o[k] = removeFalsies(v);
    }

  } else if(_.isArray(v)) {
  if(!o.hasOwnProperty(k)) o[k] = [];

  //And if it is array loop through it

  _.forEach(v, function(v1, k1) {
     o[k].push(removeFalsies(v1));
  });

  } else if (v === false) {
    o[k] = v;
  } else if (v) {
    o[k] = v;
  }
});
}

调用函数时尝试删除mapValues,因为它创建对象并转换数组因为你的密钥0被转换为密钥

while calling function just try to remove mapValues as it create object and convert array in objects because of that your key 0 is getting converted to key

removeEmptyObjects =function (obj) {
      return _(obj).pickBy(_.isObject).omitBy(_.isEmpty).assign(_.omitBy(obj, _.isObject)).value();
}

希望这对您有所帮助,未经过正确测试,只需简短描述。

Hope this will help you, not tested properly yes just short description.

这篇关于为什么lodash将我的数组转换为对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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