如果在Javascript ES6中为空,则从对象中删除子数组

时间:2017-08-29 16:38:49

标签: javascript ecmascript-6

我有一个看起来像这样的对象:

const inputObject = {
    "startDate": {
        "$gte": "2017-03-29T00:00:00.000Z",
        "$lte": "2017-08-20T00:00:00.000Z"
    },
    "$and": [
        { "$or": [
                {"details.thing": { "$in": [ "01" ] } },
                {"details.thing": { "$in": [ "01" ] } },
                {"details.thing": { "$in" : [ "01" ] } },
                {"details.thing": { "$in" : [ "01" ] } }
            ]
        },
        {
            "$or": []
        },
        {
            "$or": []
        }
    ]
}

我需要开发一个删除任何存在的空{ "$or": [] },子数组的函数。我的预期输出是:

const outputObject = {
    "startDate": {
        "$gte": "2017-03-29T00:00:00.000Z",
        "$lte": "2017-08-20T00:00:00.000Z"
    },
    "$and": [
        { "$or": [
                {"details.thing": { "$in": [ "01" ] } },
                {"details.thing": { "$in": [ "01" ] } },
                {"details.thing": { "$in" : [ "01" ] } },
                {"details.thing": { "$in" : [ "01" ] } }
            ]
        }
    ]
}

我能够为对象中的第一级数组执行此操作,但我不确定如何为第二级数组执行此操作。

3 个答案:

答案 0 :(得分:2)

使用Array.prototype.filter

const outputObject = {
  "startDate": {
    "$gte": "2017-03-29T00:00:00.000Z",
    "$lte": "2017-08-20T00:00:00.000Z"
  },
  "$and": [
    { 
      "$or": [
        {"details.thing": { "$in": [ "01" ] } },
        {"details.thing": { "$in": [ "01" ] } },
        {"details.thing": { "$in" : [ "01" ] } },
        {"details.thing": { "$in" : [ "01" ] } }
      ]
    },
    {
      "$or": []
    }
  ]
};

var filtered = outputObject.$and = outputObject.$and
  .filter(cond => cond.$or.length > 0)
;

// console.log('filtered', filtered);
// shows only the resulting array, your Object remains
// outputObject
console.log('outputObject', outputObject);

答案 1 :(得分:1)

您可以对任何深度使用迭代和递归方法。

function deepDelete(object, key) {
    Object.keys(object).forEach(function (k) {
        var i;
        if (Array.isArray(object[k])) {
            i = object[k].length;
            while (i--) {
                if (Array.isArray(object[k][i][key]) && object[k][i][key].length === 0) {
                    object[k].splice(i, 1);
                    continue;
                }
                deepDelete(object[k][i], key);
            }
            return;
        }
        if (object[k] && typeof object[k] === 'object') {
            deepDelete(object[k], key) 
        }
    });
}

var data = { startDate: { $gte: "2017-03-29T00:00:00.000Z", $lte: "2017-08-20T00:00:00.000Z" }, $and: [{ $or: [{ "details.thing": { $in: ["01"] } }, { "details.thing": { $in: ["01"] } }, { "details.thing": { $in: ["01"] } }, { "details.thing": { $in: ["01"] } }] }, { $or: [] }, { $or: [] }] };

deepDelete(data, '$or');

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

const inputObject = {
  startDate: {
    $gte: "2017-03-29T00:00:00.000Z",
    $lte: "2017-08-20T00:00:00.000Z"
  },
  $and: [
    {
      $or: [
        { "details.thing": { $in: ["01"] } },
        { "details.thing": { $in: ["01"] } },
        { "details.thing": { $in: ["01"] } },
        { "details.thing": { $in: ["01"] } }
      ]
    },
    {
      $or: []
    },
    {
      $or: []
    }
  ]
};

function isEqualToEmptyOr(obj) {
  return JSON.stringify(obj) === JSON.stringify({ $or: [] });
}

function removeOrRecursion(obj) {
  if (Array.isArray(obj)) {
    let result = [];
    obj.forEach(v => {
      if (!isEqualToEmptyOr(v)) {
        result = result.concat(removeOrRecursion(v));
      }
    });
    return result;
  } else if (typeof obj === "object") {
    let result = {};
    Object.keys(obj).forEach(key => {
      if (!isEqualToEmptyOr(obj[key])) {
        result[key] = removeOrRecursion(obj[key]);
      }
    });
    return result;
  } else {
    return obj;
  }
}

const result = removeOrRecursion(inputObject);
console.log(result);