繁体   English   中英

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

[英]filtering deep nested objects array using lodash not working correctly

我的产品结构如下图所示:

   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 ,使其只包含selCountries countries

最终产品应为:

{  
  "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 因此,解决方案也应处理这种情况,并且不应因未定义的错误而中断。

哪里有问题可以帮助我吗?

您不需要为此。 只是根据ID进行filter 如果是所有图层,请在图层上映射/每个,然后过滤国家/地区。

 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); 

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

 let product = { "name": "MyXam", "layers": [{ "countries": [{ "countryId": "1", "countryName": "ABC" }, { "countryId": "2", "countryName": "XYZ" }, { "countryId": "3", "countryName": "PQR" } ] }] }; let selCountries = [{ "countryId": "1" }, { "countryId": "3" } ]; // Extract the IDs let selCountryIds = _.map(selCountries, 'countryId'); // Filter the countries based on IDs product.layers[0].countries = _.filter(product.layers[0].countries, country => { return _.includes(selCountryIds, country.countryId); }); console.log(product); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script> 

您可以使用Array.mapArray.filter来遍历数组并根据selected countries. /地区过滤product ,而不是使用lodash selected countries.

 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); 

我的答案与31piy的答案相似,因为我首先从selCountries提取了id,然后使用过滤的结果重建了对象。 它还会根据您最近的评论检查是否在图层数组中存在国家。

 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); } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM