簡體   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