使用lodash无法正常工作过滤深层嵌套对象数组

时间:2018-03-26 11:39:32

标签: javascript underscore.js lodash

我的产品结构如下所示:

   product = {  
  "name":"MyXam",
  "layers":[  
    {  
      "countries":[  
        {  
          "countryId":"1",
          "countryName":"ABC"
        },
        {  
          "countryId":"2",
          "countryName":"XYZ"
        },
        {  
          "countryId":"3",
          "countryName":"PQR"
        }
      ]
    },
    {  
      "countries":[  
        {  
          "countryId":"5",
          "countryName":"LMN"
        },
        {  
          "countryId":"3",
          "countryName":"PQR"
        }
      ]
    }
  ]
}

选定国家:

selCountries =    [  
  {  
    "countryId":"1"
  },
  {  
    "countryId":"3"
  }
]

现在,我希望过滤product,使其仅包含countries中的selCountries

最终产品应该是:

{  
  "name":"MyXam",
  "layers":[  
    {  
      "countries":[  
        {  
          "countryId":"1",
          "countryName":"ABC"
        },
        {  
          "countryId":"3",
          "countryName":"PQR"
        }
      ]
    },
    {  
      "countries":[  
        {  
          "countryId":"3",
          "countryName":"PQR"
        }
      ]
    }
  ]
}

我使用lodash尝试过以下操作,但无效:

_.filter(product.layers, _.flow(
      _.property('countries'),
      _.partialRight(_.some, selCountries)
  ));

product动态地出现在我的应用程序中。在某些情况下,某些layers可能没有countries。所以解决方案也应该处理这种情况,不应该破坏未定义的错误。

任何可以帮助我,我出错的地方吗?

4 个答案:

答案 0 :(得分:2)

你不应该为此而需要lodash。只需基于ID filter。如果对于所有图层,请在图层上映射/ forEach并过滤国家/地区。



const product = {  
  "name":"MyXam",
  "layers":[  
    {  
      "countries":[  
        {  
          "countryId":"1",
          "countryName":"ABC"
        },
        {  
          "countryId":"2",
          "countryName":"XYZ"
        },
        {  
          "countryId":"3",
          "countryName":"PQR"
        }
      ]
    }
  ]
}

const selCountries = [  
  {  
    "countryId":"1"
  },
  {  
    "countryId":"3"
  }
];

const indices = selCountries.map(e => e.countryId); // Just IDs plz.
product.layers.forEach(layer => {
   if (layer.countries == null)
       return;

   layer.countries = layer.countries.filter(e => 
       indices.some(i => i == e.countryId)
   );
});
console.log(product);




答案 1 :(得分:1)

您可以创建一个临时数组,其中包含所选国家/地区的ID,然后根据它过滤国家/地区。请注意,它会就地修改原始对象。

Itemssource
combobox

答案 2 :(得分:1)

您可以使用lodashArray.map来迭代数组并根据Array.filter <过滤product,而不是使用selected countries. / p>

&#13;
&#13;
var product = {  
  "name":"MyXam",
  "layers":[  
    {  
      "countries":[  
        {  
          "countryId":"1",
          "countryName":"ABC"
        },
        {  
          "countryId":"2",
          "countryName":"XYZ"
        },
        {  
          "countryId":"3",
          "countryName":"PQR"
        }
      ]
    }
  ]
}


var selCountries =    [  
  {  
    "countryId":"1"
  },
  {  
    "countryId":"3"
  }
];

product.layers = product.layers.map(function (layer) {
  return layer.countries.filter(function (country) {
    return selCountries.some(function(selCountry) {
      return selCountry.countryId === country.countryId;
    });
  });
});

console.log(product);
&#13;
&#13;
&#13;

答案 3 :(得分:1)

我的回答与31piy类似,我首先从selCountries中提取出ID,然后使用过滤后的结果重建对象。它还会根据您最近的评论检查图层数组中是否有国家/地区。

&#13;
&#13;
product = {"name":"MyXam","layers":[{"countries":[{"countryId":"1","countryName":"ABC"},{"countryId":"2","countryName":"XYZ"},{"countryId":"3","countryName":"PQR"}]},{"countries":[{"countryId":"5","countryName":"LMN"},{"countryId":"3","countryName":"PQR"}]}]}
const selCountries=[{"countryId":"1"},{"countryId":"3"}];

if (product.layers.length) {
  const selCountriesArr = selCountries.map(el => el.countryId);
  const newLayers = product.layers.map(obj => {
    const countries = obj.countries.filter(el => selCountriesArr.includes(el.countryId));
    return { countries };
  });
  const filteredProduct = { ...product, layers: newLayers };
  console.log(filteredProduct);
}
&#13;
&#13;
&#13;