How to determine if Javascript array contains an object with an attribute that equals a given value?

后端 未结 25 1162
借酒劲吻你
借酒劲吻你 2020-11-22 08:17

I have an array like

vendors = [{
    Name: \'Magenic\',
    ID: \'ABC\'
  },
  {
    Name: \'Microsoft\',
    ID: \'DEF\'
  } // and so on... 
];


        
相关标签:
25条回答
  • 2020-11-22 08:45

    My approach to solving this problem is to use ES6 and creating a function that does the check for us. The benefit of this function is that it can be reusable through out your project to check any array of objects given the key and the value to check.

    ENOUGH TALK, LET'S SEE THE CODE

    Array

    const ceos = [
      {
        name: "Jeff Bezos",
        company: "Amazon"
      }, 
      {
        name: "Mark Zuckerberg",
        company: "Facebook"
      }, 
      {
        name: "Tim Cook",
        company: "Apple"
      }
    ];
    

    Function

    const arrayIncludesInObj = (arr, key, valueToCheck) => {
      let found = false;
    
      arr.some(value => {
        if (value[key] === valueToCheck) {
          found = true;
          return true; // this will break the loop once found
        }
      });
    
      return found;
    }
    

    Call/Usage

    const found = arrayIncludesInObj(ceos, "name", "Tim Cook"); // true
    
    const found = arrayIncludesInObj(ceos, "name", "Tim Bezos"); // false
    
    0 讨论(0)
  • 2020-11-22 08:46

    2018 edit: This answer is from 2011, before browsers had widely supported array filtering methods and arrow functions. Have a look at CAFxX's answer.

    There is no "magic" way to check for something in an array without a loop. Even if you use some function, the function itself will use a loop. What you can do is break out of the loop as soon as you find what you're looking for to minimize computational time.

    var found = false;
    for(var i = 0; i < vendors.length; i++) {
        if (vendors[i].Name == 'Magenic') {
            found = true;
            break;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:46

    Correct me if i'm wrong.. i could have used forEach method like this,

    var found=false;
    vendors.forEach(function(item){
       if(item.name === "name"){
           found=true;
    
       }
    });
    

    Nowadays i'm used to it ,because of it simplicity and self explanatory word. Thank you.

    0 讨论(0)
  • 2020-11-22 08:48

    Unless you want to restructure it like this:

    vendors = {
        Magenic: {
          Name: 'Magenic',
          ID: 'ABC'
         },
        Microsoft: {
          Name: 'Microsoft',
          ID: 'DEF'
        } and so on... 
    };
    

    to which you can do if(vendors.Magnetic)

    You will have to loop

    0 讨论(0)
  • 2020-11-22 08:49

    You cannot without looking into the object really.

    You probably should change your structure a little, like

    vendors = {
        Magenic:   'ABC',
        Microsoft: 'DEF'
    };
    

    Then you can just use it like a lookup-hash.

    vendors['Microsoft']; // 'DEF'
    vendors['Apple']; // undefined
    
    0 讨论(0)
  • 2020-11-22 08:49

    Many answers here are good and pretty easy. But if your array of object is having a fixed set of value then you can use below trick:

    Map all the name in a object.

    vendors = [
        {
          Name: 'Magenic',
          ID: 'ABC'
         },
        {
          Name: 'Microsoft',
          ID: 'DEF'
        }
    ];
    
    var dirtyObj = {}
    for(var count=0;count<vendors.length;count++){
       dirtyObj[vendors[count].Name] = true //or assign which gives you true.
    }
    

    Now this dirtyObj you can use again and again without any loop.

    if(dirtyObj[vendor.Name]){
      console.log("Hey! I am available.");
    }
    
    0 讨论(0)
提交回复
热议问题