【问题标题】:Check if value is present in an object or array检查值是否存在于对象或数组中
【发布时间】:2017-02-08 15:16:10
【问题描述】:

我有一些如下格式的数据

var list2 = [
  {
    items:{ 
      val: 'a'
    }
  },
  {
    items:[
      {
        val: 'b'
      },
      {
        val: 'c'
      },
    ]
  }
];

例如,我需要检查列表中是否包含带有val = 'b' 的项目。

如您所见,这是一个对象数组,每个对象都有一个 items 属性,可以是单个对象,也可以是对象数组。

【问题讨论】:

    标签: javascript arrays object find lodash


    【解决方案1】:

    洛达什

    对于返回true 的简单测试,如果至少有一项与我们搜索的属性匹配,则_.some 可以工作。

    function test(arr, matches) {
      return _.some(arr, obj => {
        var items = obj.items;
        // make non-array items an array.
        return _.some(!_.isArray(items) ? [items]: items, matches);
      });
    }
    
    var hasValA = test(list2, { val: 'a' });
    

    function test(arr, matches) {
      return _.some(arr, obj => {
        var items = obj.items;
        return _.some(!_.isArray(items) ? [items]: items, matches);
      });
    }
    
    var list1 = [{
        items: [{
          val: 'a'
        }]
      }],
      list2 = [{
        items: {
          val: 'a'
        }
      }, {
        items: [{
          val: 'b'
        }, {
          val: 'c'
        }, ]
      }],
      list3 = [{
        items: [{
          val: 'b'
        }]
      }];
    
    var matches = {
      val: 'a'
    };
    
    console.log("list1:", test(list1, matches));
    console.log("list2:", test(list2, matches));
    console.log("list3:", test(list3, matches));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

    ES6

    这在普通 ES6 中也可以在

    的帮助下实现
    function test(arr, matches) {
      return arr.some(obj => {
        var items = obj.items;
        if (!Array.isArray(items)) items = [items];
        return items.some(obj => {
          return Object.keys(matches).every(key => matches[key] === obj[key]);
        });
      });
    }
    
    var hasValA = test(list2, { val: 'a' });
    

    function test(arr, matches) {
      return arr.some(obj => {
        var items = obj.items;
        if (!Array.isArray(items)) items = [items];
        return items.some(obj => {
          return Object.keys(matches).every(key => matches[key] === obj[key]);
        });
      });
    }
    
    var list1 = [{
        items: [{
          val: 'a'
        }]
      }],
      list2 = [{
        items: {
          val: 'a'
        }
      }, {
        items: [{
          val: 'b'
        }, {
          val: 'c'
        }, ]
      }],
      list3 = [{
        items: [{
          val: 'b'
        }]
      }];
    
    var matches = {
      val: 'a'
    };
    
    console.log("list1:", test(list1, matches));
    console.log("list2:", test(list2, matches));
    console.log("list3:", test(list3, matches));

    两种解决方案都适用于要匹配的更复杂的对象。

    test(list2, { val: 'a', other: 2, /* ... */ });
    

    【讨论】:

    • 谢谢。我已经使用了“一些”功能,以及您检查单个项目并将其转换为数组的方法,现在我的项目中使用了它。
    • @JonathanShutt 你应该接受一个答案,或者如果任何一个对你有足够的帮助,你可以提供你自己的答案并接受它。如果仍未解决,我鼓励您在答案的评论部分询问更多详细信息。
    【解决方案2】:

    您可以使用 flatMap 从数组中提取和展平项目,然后使用 some:

    var result = _(list2)
        .flatMap('items')
        .some({val: 'a'})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-22
      • 2020-11-30
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多