【问题标题】:What's a clean way to verify a few objects in an array?验证数组中的几个对象的干净方法是什么?
【发布时间】:2020-06-28 21:19:11
【问题描述】:

我试图找到一种有效的方法来检查数组是否包含我期望的对象。在下面的示例中,我希望 fruit 中的对象具有 type = apple, banana, and orange。例如,如果缺少banana,那么它应该返回false

这是一个非常基本的解决方案:

const fruit = [
  {'type': 'apple', 'color': 'red', 'quantity': 10},
  {'type': 'banana', 'color': 'yellow', 'quantity': 9},
  {'type': 'orange', 'color': 'orange', 'quantity': 3}
  ];

let fruitsToCheck = ['apple', 'banana', 'orange'];
let fruitsThatExist = [];

fruit.forEach( fruit => {
  fruitsThatExist.push(fruit.type);
});

fruitsToCheck = fruitsToCheck.sort().toString();
fruitsThatExist = fruitsThatExist.sort().toString();

const allExist = fruitsToCheck === fruitsThatExist;

我的解决方案有效,但效率不高。有什么更好的方法来解决这个问题?

【问题讨论】:

标签: javascript arrays node.js validation object


【解决方案1】:

查看array.everyarray.some

const allExist = fruitsToCheck.every(type =>
  fruit.some(fruit => fruit.type === type)
)

【讨论】:

    【解决方案2】:

    const fruits = [
      {'type': 'apple', 'color': 'red', 'quantity': 10},
      {'type': 'banana', 'color': 'yellow', 'quantity': 9},
      {'type': 'orange', 'color': 'orange', 'quantity': 3}
      ];
      
     console.log(fruits.find(fruit => fruit.type === 'apple' || fruit.type === 'banana' || fruit.type === 'orange') !== undefined)

    如果您需要检查所有水果苹果、香蕉和橙子是否存在

    const fruits = [
          {'type': 'apple', 'color': 'red', 'quantity': 10},
          {'type': 'banana', 'color': 'yellow', 'quantity': 9},
          {'type': 'orange', 'color': 'orange', 'quantity': 3}
          ];
          
    const map = {
      'apple': 0,
      'banana': 0,
      'orange': 0
    }
         
    console.log(fruits.reduce((sum, fruit) => sum + (map[fruit.type] === 0 ? ++map[fruit.type] : 0), 0) === 3)

    【讨论】:

    • 这不是只找到一个对象吗?我认为它不会检查所有三个是否存在。
    • 它会找到第一个对象的类型为apple、banana或orange,并立即返回。
    • 添加一个检查所有水果类型存在的例子
    【解决方案3】:

    您可以收集Set 中的类型并检查Set#has 作为Array#every 的回调。

    const
        fruit = [{ type: 'apple', color: 'red', quantity: 10 }, { type: 'banana', color: 'yellow', quantity: 9 }, { type: 'orange', color: 'orange', quantity: 3 }],
        fruitsToCheck = ['apple', 'banana', 'orange'],
        allExist = fruitsToCheck.every(
            Set.prototype.has,                     // callback
            new Set(fruit.map(({ type }) => type)) // thisArg
        );
    
    console.log(allExist);

    【讨论】:

      【解决方案4】:

      every 方法测试数组中的所有元素是否通过提供的函数实现的测试。它返回一个布尔值。 doc

      some 方法测试数组中的至少一个元素是否通过了提供的函数实现的测试。它返回一个布尔值。 doc

      const fruit = [
        {'type': 'apple', 'color': 'red', 'quantity': 10},
        {'type': 'banana', 'color': 'yellow', 'quantity': 9},
        {'type': 'orange', 'color': 'orange', 'quantity': 3}
      ];
      
      let fruitsToCheck = ['apple', 'banana', 'orange'];
      
      // solution - 1 
      const allExist = fruitsToCheck.every(t => fruit.some(f => f.type === t));
      
      // soution - 2
      const allExist = fruitsToCheck.every(f => fruit.map(fr => fr.type).includes(f))
      
      // soution - 3 
      const allExist = [...new Set(fruit.map(f => f.type))].length === fruitsToCheck.length;
      
      
      
      

      【讨论】:

        【解决方案5】:

        您可以使用every 函数来检查水果对象中的每种类型是否在您的水果上都是included 以检查数组。

        const fruit = [
          {'type': 'apple', 'color': 'red', 'quantity': 10},
          {'type': 'banana', 'color': 'yellow', 'quantity': 9},
          {'type': 'orange', 'color': 'orange', 'quantity': 3}
          ];
          
        const fruitsToCheck = ['apple', 'banana', 'orange'];
        
        const allExist = fruit.every(({type})=> fruitsToCheck.includes(type))
        
        console.log(allExist);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多