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

后端 未结 25 1219
借酒劲吻你
借酒劲吻你 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 09:06

    You can try this its work for me.

    const _ = require('lodash');
    
    var arr = [
      {
        name: 'Jack',
        id: 1
      },
      {
        name: 'Gabriel',
        id: 2
      },
      {
        name: 'John',
        id: 3
      }
    ]
    
    function findValue(arr,value) {
      return _.filter(arr, function (object) {
        return object['name'].toLowerCase().indexOf(value.toLowerCase()) >= 0;
      });
    }
    
    console.log(findValue(arr,'jack'))
    //[ { name: 'Jack', id: 1 } ]
    

提交回复
热议问题