如何重构此代码以提供属性名称(字符串)?

huangapple go评论62阅读模式
英文:

How to refactor this to provide the property name (string)?

问题

以下是可运行的代码。问题特别涉及到重构,以提供用于由 isSame 进行比较的属性名称字符串。可能更名为 isPropValueSame

import * as _ from 'lodash';

const diff = _.differenceWith(sourceList, comparatorList, this.isSame);

isSame = (objA: any, objB: any) => (_.has(objA, 'id') && _.has(objB, 'id') ? objA['id'] === objB['id'] : false);

希望传递字段/属性名称字符串,例如 this.isSame('id'));

objAobjB 将是列表 sourceListcomparatorList 中的项目,并且列表可能如下所示:

const sourceList = [
    { id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
    { id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
    { id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];
const comparatorList = [
    { id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
    //{ id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
    { id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];

使用上述测试数据(注意 comparatorList 已将第二个项目注释掉),那么比较器的输出将返回 id 等于 2 的项目,因为它在委托函数 isSame 的比较中未找到它。

英文:

The following is working code. The question is specifically about refactoring to provide the string for property name used to compare by isSame. Perhaps better renamed to isPropValueSame.

import * as _ from 'lodash';

const diff = _.differenceWith(sourceList, comparatorList, this.isSame);

isSame = (objA: any, objB: any) => (_.has(objA, 'id') && _.has(objB, 'id') ? objA['id'] === objB['id'] : false);

Looking to pass field/prop name string like this.isSame('id'));

objA and objB would be the items from lists: sourceList and comparatorList and lists might look like:

const sourceList = [
    { id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
    { id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
    { id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];
const comparatorList = [
    { id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
    //{ id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
    { id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];

With above test case data (note comparatorList has second item commented out), then the output of comparator would return the item where id equals 2 because it does not find it during comparison by delegate function isSame.

答案1

得分: 1

不确定你是否只是在寻找对键进行封装的方法 - 似乎类型安全对你不是个问题,所以可以简单处理为:

const isSame = (key: string) => (objA: unknown, objB: unknown) =>
  _.has(objA, key) && _.has(objB, key) && _.get(objA, key) === _.get(objB, key);

Sandbox

如果你可以不考虑键的类型检查,那么这就足够了,但如果需要的话,我会参考你之前提到的递归访问路径的答案

英文:

Not sure if you're looking for more than just a closure around the key – doesn't seem like type-safety is a concern for you so could be as simple as:

const isSame = (key: string) => (objA: unknown, objB: unknown) =>
  _.has(objA, key) && _.has(objB, key) && _.get(objA, key) === _.get(objB, key);

Sandbox

If you can live without having type checking on the key then that would suffice, but if you need it then I'd refer you to this previous answer on recursive access paths.

答案2

得分: 1

一种选择是使用闭包,将感兴趣的属性传递给它:

const isSame = (prop) => {
  const issame = (a, b) => (_.has(a, prop) && _.has(b, prop) ? a[prop] === b[prop] : false);
  return issame;
};

const someValue = 42;
const prop3Value = 3;

const sourceList = [
  { id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
  { id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
  { id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];
const comparatorList = [
  { id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
  // { id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
  { id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];

console.log(_.differenceWith(sourceList, comparatorList, isSame("id")));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
英文:

One option is to use a closure to which you pass the prop/attribute of interest:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const isSame = (prop) =&gt; {
  const issame = (a, b) =&gt; (_.has(a, prop) &amp;&amp; _.has(b, prop) ? a[prop] === b[prop] : false);
  return issame;
};

const someValue = 42;
const prop3Value = 3;

const sourceList = [
  { id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
  { id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
  { id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];
const comparatorList = [
  { id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
  // { id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
  { id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];

console.log(_.differenceWith(sourceList, comparatorList, isSame(&quot;id&quot;)));

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 22:14:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定