删除嵌套数组中的不匹配对象

时间:2016-06-02 09:01:56

标签: javascript arrays json nested lodash

我在嵌套数组中以通用方式使用splice方法时遇到了问题。 所以我想要的是删除“bigArray”的“nestedArray”中的对象,如果“filterArray”没有对象的引用。

我做了一个我想做的例子。 如果你们有一个解决方案,我可以用原生的javascript来改进它,或者让Lodash随意提供输入。我真的被卡住了。

预期结果应为:

 var sort = this;
var init = function(){
  sort.outObjects();
};
sort.filterArray = { "filter" : [
  { "id": 3, "groupId": 1 }, 
  { "id": 2, "groupId": 2 }]};

sort.unsortedArray = { "bigArray" : [
  {"id": 1, "text" : "This should be visible when done",
    "nestedArray": [
      { "id": 1, "nestedText":"Sort this one out!" }, 
      { "id": 2, "nestedText": "Sort me out!" }, 
      { "id": 3, "nestedText": "This one should  be visible in the coming array"}]},

  { "id": 2, "text" : "This should be visible when done",
    "nestedArray": [
      { "id": 1, "nestedText": "Sort me out!" }, 
      { "id": 2, "nestedText": "This one should  be visible in the coming array" }, 
      {"id": 3, "nestedText": "Sort this one out!" }]}
]},

sort.outObjects = function (){

  //Check that we got the objects in same scope
  if(sort.filterArray && sort.unsortedArray){

    //Loop through "bigArray" object
    for( var i = 0; i< sort.unsortedArray.length; i++){

    console.log("sort.unsortedArray.length : ", sort.unsortedArray.length);

      //Loop through each "nestedArray":s objects
      for( var j = 0; j< sort.unsortedArray[i].nestedArray.length; j++){
      console.log("sort.unsortedArray[i].nestedArray.length : ", sort.unsortedArray[i].nestedArray.length);

        //Loop through filterArray object and compare each object in nested array, if they dont match, delete them.

       for( var k = 0; k< sort.filterArray.length; k++){
        console.log("sort.filterArray.length : ", sort.filterArray.length);

        if(sort.filterArray[k].id != sort.unsortedArray[i].nestedArray[j].id){

         //Delete unmatching object from unsortedArray
          sort.unsortedArray[i].nestedArray.splice(j,1);

         console.log("sort.unsortedArray after splice : ", sort.unsortedArray);
       }
      }
    }
  }
  }
  else{
    console.log("Missing connection to object",sort.filterArray, sort.unsortedArray);
  }
}

init();

这是一个codepen

{{1}}

2 个答案:

答案 0 :(得分:1)

您可以生成一个对象以便更快地访问并测试最终过滤。

生成后,您可以迭代数据,检查是否需要进行更改并应用过滤。

编辑:建议保留多个ID。

var filterArray = { "filter": [{ "id": 3, "groupId": 1 }, { "id": 2, "groupId": 2 }] },
    unsortedArray = { "bigArray": [{ "id": 1, "text": "This should be visible when done", "nestedArray": [{ "id": 1, "nestedText": "Sort this one out!" }, { "id": 2, "nestedText": "Sort me out!" }, { "id": 3, "nestedText": "This one should  be visible in the coming array" }] }, { "id": 2, "text": "This should be visible when done", "nestedArray": [{ "id": 1, "nestedText": "Sort me out!" }, { "id": 2, "nestedText": "This one should  be visible in the coming array" }, { "id": 3, "nestedText": "Sort this one out!" }] }] },
    filterObject = Object.create(null);

filterArray.filter.forEach(function (a) {
    filterObject[a.groupId] = filterObject[a.groupId] || Object.create(null);
    filterObject[a.groupId][a.id] = true;
});

unsortedArray.bigArray.forEach(function (a) {
    if (a.id in filterObject) {
        a.nestedArray = a.nestedArray.filter(function (b) {
            return filterObject[a.id][b.id];
        });
    }
});
console.log(filterObject);
console.log(unsortedArray);

答案 1 :(得分:0)

这将是我的解决方案;

var fa = { "filter" : [{ "id": 3, "groupId": 1 },{ "id": 2, "groupId": 2 }]},
   usa = { "bigArray" : [{"id": 1, "text" : "This should be visible when done", "nestedArray": [{ "id": 1, "nestedText":"Sort this one out!" },{ "id": 2, "nestedText": "Sort me out!" },{ "id": 3, "nestedText": "This one should  be visible in the coming array"}]},{ "id": 2, "text" : "This should be visible when done","nestedArray": [{ "id": 1, "nestedText": "Sort me out!" },{ "id": 2, "nestedText": "This one should  be visible in the coming array" },{"id": 3, "nestedText": "Sort this one out!" }]}]},
    sa = fa.filter.reduce((p,c) => { var f = p.find(f => f.id == c.groupId);
                                     f && (f.nestedArray = f.nestedArray.reduce((n,o) => o.id == c.id ? n.concat(o) : n,[]));
    	                             return p }
                                   ,usa.bigArray);
console.log(sa);