找到值并从深层嵌套的对象

时间:2017-07-13 18:39:45

标签: javascript arrays ecmascript-6 underscore.js lodash

我浏览了很多问题,找出了在深层嵌套的对象数组中查找索引的逻辑,但我并没有发现它对我的要求有用。

Javascript,Lodash / Underscore 中搜索解决方案也很好。 让我简单地说出整个要求,希望我找到解决这个问题的方法。

要求:
我有一个对象数组

arrObj =[
        {
            "id":3208,
            "name":"List",
            "issueResponses":[

            ],
            "isActive":false
        },
        {
            "id":3209,
            "name":"Me",
            "issueResponses":[
            ],
            "isActive":false
        },
        {
            "id":3314,
            "name":"SNew",
            "issueResponses":[

            ],
            "isActive":false
        },
        {
            "id":3315,
            "name":"Olive",
            "issueResponses":[
                {
                    "id":3282,
                    "name":"related to Olive",
                    "issueResponses":[

                    ],
                    "isActive":false
                },
                {
                    "id":3316,
                    "name":"My olives are not yet picked",
                    "issueResponses":[
                        {
                            "id":3317,
                            "name":"Pickup Not Done",
                            "issueResponses":[

                            ],
                            "isActive":false
                        }
                    ]
                }
            ]
        }
    ]

正如我们可以看到它的深层嵌套,我有另一个数组

delValue = [3317,3282] ,明天它可能是这些深层嵌套中的任何东西。 我必须在arrObj中找到这些delValue数组并删除所有那些具有" id"作为这些价值观。

我试图以一种通用的方式解决这个问题,它可以支持任意数量的深度搜索等级和删除对象。

如果需要更多信息,请在此帮助我。

4 个答案:

答案 0 :(得分:0)

继续检查issueResponses - 就像这样:

function deleteIssueResponses(issues, ids) {
    for (let i = issues.length; i >= 0; i--) {
        if (issues[i].issueResponses.length) {
            deleteIssueResponses(issues[i].issueResponses, ids);
        }

        if (ids.contains(issues[i].id)) {
            issues.splice(i, 1);
        }
    }
}

并称之为:

deleteIssueResponses(arrObj, [3317,3282]);

答案 1 :(得分:0)

使用一些递归解决这个问题应该很简单。此函数将删除传递给它的数组中给定的id,或者调用自身对嵌套数组执行相同的操作。

function deleteIds(arr, ids) {
  for (let i = 0; i < arr.length; i++) {
    if (ids.indexOf(arr[i].id) !== -1) {
      arr.splice(i, 1);
      i--;
    } else {
      deleteIds(arr[i].issueResponses, ids);
    }
  }
}

在问题中使用deleteIds(arrObj, delValue)致电。

答案 2 :(得分:0)

您可以使用过滤器来实现此目的:

function removeDeletions(array, deletion) {
    return array.filter(el => {
        if (Array.isArray(el.issueResponses)) el.issueResponses = removeDeletions(el.issueResponses, deletion);
        return ! deletion.includes(el.id);
    })
}

样本:

&#13;
&#13;
let arrObj = [{
    "id": 3208,
    "name": "List",
    "issueResponses": [

    ],
    "isActive": false
  },
  {
    "id": 3209,
    "name": "Me",
    "issueResponses": [],
    "isActive": false
  },
  {
    "id": 3314,
    "name": "SNew",
    "issueResponses": [

    ],
    "isActive": false
  },
  {
    "id": 3315,
    "name": "Olive",
    "issueResponses": [{
        "id": 3282,
        "name": "related to Olive",
        "issueResponses": [

        ],
        "isActive": false
      },
      {
        "id": 3316,
        "name": "My olives are not yet picked",
        "issueResponses": [{
          "id": 3317,
          "name": "Pickup Not Done",
          "issueResponses": [

          ],
          "isActive": false
        }]
      }
    ]
  }
]
let delValue = [3317, 3282];

function removeDeletions(array, deletion) {
  return array.filter(el => {
    if (Array.isArray(el.issueResponses)) el.issueResponses = removeDeletions(el.issueResponses, deletion);
    return !deletion.includes(el.id);
  })
}

console.log(removeDeletions(arrObj, delValue));
&#13;
&#13;
&#13;

答案 3 :(得分:0)

我们将object-scan用于此类基本数据处理任务。一旦掌握了使用方法,它就会非常强大。这是您可以回答问题的方式:

const objectScan = require('object-scan');

const prune = (values, data) => objectScan(['**[*].id'], {
  rtn: 'count',
  filterFn: ({ parents, key, value }) => {
    if (values.includes(value)) {
      parents[1].splice(key[key.length - 2], 1);
      return true;
    }
    return false;
  }
})(data);

const arrObj = [{"id":3208,"name":"List","issueResponses":[],"isActive":false},{"id":3209,"name":"Me","issueResponses":[],"isActive":false},{"id":3314,"name":"SNew","issueResponses":[],"isActive":false},{"id":3315,"name":"Olive","issueResponses":[{"id":3282,"name":"related to Olive","issueResponses":[],"isActive":false},{"id":3316,"name":"My olives are not yet picked","issueResponses":[{"id":3317,"name":"Pickup Not Done","issueResponses":[],"isActive":false}]}]}];

console.log(prune([3317, 3282], arrObj)); // return number of deletes
// => 2

console.log(JSON.stringify(arrObj));
// => [{"id":3208,"name":"List","issueResponses":[],"isActive":false},{"id":3209,"name":"Me","issueResponses":[],"isActive":false},{"id":3314,"name":"SNew","issueResponses":[],"isActive":false},{"id":3315,"name":"Olive","issueResponses":[{"id":3316,"name":"My olives are not yet picked","issueResponses":[]}]}]