Lodash isEqual 与特定 属性 小写比较

Lodash isEqual comparison with specific property lowercase

是否有 lodash 比较两个对象的方法,其中某个 属性 以不区分大小写的方式进行比较

const obj1 = { name: 'Dave', course: 'Math' };
const obj2 = { name: 'dave', course: 'Math' };

name 的比较不区分大小写,但其他属性

则严格相等

正在考虑以下内容(显然仅作为示例):

var result = _.isEqual(
  _.omit(obj1, ['creation', 'deletion']),
  _.omit(obj2, ['creation', 'deletion'])
);

使用_.isEqualWith()将对象与定制器进行比较。如果键(第 3 个自定义参数)是 name 进行不敏感比较,对于其他键 return undefined 使用默认比较。

const obj1 = { name: 'Dave', course: 'Math' };
const obj2 = { name: 'dave', course: 'Math' };

const insensetiveStrCompare = (v1, v2) =>
  !v1.localeCompare(v2, undefined, { sensitivity: 'accent' }) // returns 0 if equal, and !0 === true

const result = _.isEqualWith(
  obj1, 
  obj2, 
  (v1, v2, key) => key === 'name' ? 
    insensetiveStrCompare(v1, v2)
    :
    undefined
)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>