【问题标题】:Lodash find for Array of objectsLodash 查找对象数组
【发布时间】:2020-02-25 16:48:29
【问题描述】:

我有以下 JsonArray:

arr = [
      {
        "name": "test",
        "alias": "alias1",
        "type": 1
      },
      {
        "name": "test",
        "type": 0
      },
      {
        "name": "abc",
        "alias": "alias2",
        "type": 1
      }
    ]

我想使用变量值(可能包含别名/键)来查找。所以基本上find的首选应该是别名,如果没有找到具有相同值的别名,那么它应该在“name”中搜索,而“alias”不存在

通常情况下会这样:

_.find(arr, {
 alias: value 
})

但如果没有找到 alias=value,我希望代码返回 name = value 的 obj

1) 例如:值 = "alias1" 预期==>

{
    "name": "test",
    "alias": "alias1",
    "type": 1
  }

2) 例如:值 = “测试” 预期==>

{
        "name": "test",
        "type": 0
      }

【问题讨论】:

  • “我有一个以下 JsonArray” - 不,你没有。这是一个对象数组 (What is the difference between JSON and Object Literal Notation?)。
  • 我不太清楚这里问的是什么。对于这个示例数组arr,正确答案是什么?是1吗?
  • 基本上我想以这样一种方式使用_.find,它首先根据“别名”找到。如果未找到,则查找“名称”

标签: javascript underscore.js lodash


【解决方案1】:

您需要使用 find(_.find()Array.find())来查找 alias 匹配,如果没有找到,请再次使用 find 来查找 name 匹配:

const findAliasOrName = (value, arr) =>
  _.find(arr, { alias: value }) || _.find(arr, { name: value });

const arr = [{"name":"test","type":0},{"name":"test","alias":"alias1","type":1},{"name":"abc","alias":"alias2","type":1}]

console.log(findAliasOrName('alias1', arr));
console.log(findAliasOrName('test', arr));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

【讨论】:

    【解决方案2】:

    你可以这样做:

    如果 alias 键在 obj 中可用,则将值与 alias 匹配,否则使用 三元将值与 name 键匹配运营商

    var arr = [
          {
            "name": "test",
            "alias": "alias1",
            "type": 1
          },
          {
            "name": "test",
            "type": 0
          },
          {
            "name": "abc",
            "alias": "alias2",
            "type": 1
          }
        ];
    
    const findValue = (arr, value) => {
        return _.find(arr, (elem) => {
            return elem.alias ? elem.alias === value : elem.name === value;
        });
    }
    
    console.log(findValue(arr, 'alias1'));
    console.log(findValue(arr, 'test'));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

    【讨论】:

      【解决方案3】:

      您需要采用自定义回调,该回调采用两个可能的属性来检查值。

      function find(array, value) {
          return _.find(array, o => o.alias === value || o.name === value)
      }
      
      var array = [{ name: "abc", alias: "abc_alias", type: 1 }, { name: "tofind", type: 2 }, { name: "def", alias: "al1", type: 3 }];
      
      console.log(find(array, 'abc_alias'));
      console.log(find(array, 'tofind'));
      <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js">

      【讨论】:

      • 我想优先考虑“别名”而不是“名称”。所以基本上对于代码[ { "name": "value", "type": 0 }, { "name": "abc", "alias": "value", "type": 1 } ] 代码。我希望它返回代码 { "name": "abc", "alias": "value", "type": 1 }
      • 以上应该是这样工作的。如果您需要不同的偏好,请更改条件的顺序。
      • 我认为关键在于 OP 想要在 Ori Drori 的答案中找到的行为:找到第一个匹配 alias 的行为,如果没有,找到第一个匹配 name 的行为。此答案返回与 either aliasname 匹配的第一个。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 2016-08-10
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多