lodash提供了几个可以根据对象名获取数组下标的方法,如findIndex和indexOf。

  1. 使用findIndex方法:

    const _ = require('lodash');
    
    const array = [{ name: 'John' }, { name: 'Jane' }, { name: 'Bob' }];
    
    const index = _.findIndex(array, { name: 'Jane' });
    console.log(index); // 输出1
    
  2. 使用indexOf方法:

    const _ = require('lodash');
    
    const array = [{ name: 'John' }, { name: 'Jane' }, { name: 'Bob' }];
    
    const index = _.indexOf(array, _.find(array, { name: 'Jane' }));
    console.log(index); // 输出1
    

这些方法都是根据对象的属性值来判断是否匹配,如果匹配成功,则返回数组中该对象的下标

lodash根据对象名获取数组下标

原文地址: https://www.cveoy.top/t/topic/ibd6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录