【问题标题】:Find Duplicate value From Collection Using Lodash使用 Lodash 从集合中查找重复值
【发布时间】:2018-09-26 03:50:43
【问题描述】:

使用 Lodash 我试图查找集合中是否存在值。

如果存在,我想返回true,否则返回false

 const d = [{
     "country": "India",
     "_id": ObjectId("5ad47b639048dd2367e95d48"),
     "cities": []
 }, {
     "country": "Spain",
     "_id": ObjectId("5ad47b639048dd2367e95d49"),
     "cities": []
 }];

片段代码

Countries = ['India', 'Spain']
if (_.has(d, Countries)) {
    console.log(true);
} else {
    console.log(false);
}

但它总是返回 False。不建议我使用lodash 如果可能的话,谁能提出更好的方法。

【问题讨论】:

    标签: javascript arrays json lodash


    【解决方案1】:

    您可以结合使用someincludes 方法。如果items 中的任何项目包含来自countries 数组的国家/地区,它将返回true

    const items =  [
        {
            "country": "India",
            "_id": 'ObjectId("5ad47b639048dd2367e95d48")',
            "cities": []
        },
        {
            "country": "Spain",
            "_id": 'ObjectId("5ad47b639048dd2367e95d49")',
            "cities": []
        }
    ];
    
    const countries = ['India', 'Spain'];
    const includes = _.some(items, item => _.includes(countries , item.country));
    console.log(includes);
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>

    【讨论】:

      【解决方案2】:

      你也可以在没有 Lodash 的情况下使用 filter 函数中内置的 Javascript 来做到这一点

      d.filter(item => item.country === "Spain"); //Would return an array of objects where the country's name property is Spain!
      

      如果我们想将其设为布尔值,我们可以将其声明为变量并断言其长度大于 0,如下所示:

      let isSpanishCountry = d.filter(item => item.country === "Spain").length > 0; // console.log(isSpanishCountry) => true
      

      【讨论】:

      • 但在比较时有一个数组,而不是单个值
      • 非常真实!我错过了 lodash _.some_.includes 是要走的路!
      【解决方案3】:

      ES6

      您可以使用array.some()array.includes() 来检查数组中的重复条目。

      演示

      const d = [{
        "country": "India",
        "_id": "5ad47b639048dd2367e95d48",
        "cities": []
      }, {
        "country": "Spain",
        "_id": "5ad47b639048dd2367e95d49",
        "cities": []
      }];
      
      const Countries = ['India', 'Spain'];
      
      console.log(d.some(({country}) => Countries.includes(country)))
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        【解决方案4】:

        灵感来自@Christian Bartram

        var cities = [
          {city: "Beijing",bOn:false},
          {city: "Shanghai",bOn:false},
          {city: "Chongqing",bOn:false},
          {city: "Guangzhou",bOn:false}, {city: "Guangzhou",bOn:true},
          {city: "Xi'an",bOn:false}
        ];
        
        //Find duplicate element values in the array
        _(cities).groupBy(x => x.city).pickBy(x => x.length > 1).keys().value()
        

        返回

        ['广州']

        //Find duplicate objects in an array
        _.filter(_(cities).groupBy(x => x.city).value(),x => x.length > 1)
        

        返回

        [[{"city":"Guangzhou","bOn":false},{"city":"Guangzhou","bOn":true}]]

        【讨论】:

          猜你喜欢
          • 2019-12-31
          • 1970-01-01
          • 2016-11-21
          • 1970-01-01
          • 1970-01-01
          • 2011-08-28
          • 1970-01-01
          • 2013-06-19
          • 2019-08-05
          相关资源
          最近更新 更多