【问题标题】:Find a key value based on a match in Javascript根据 Javascript 中的匹配项查找键值
【发布时间】:2021-03-12 23:59:45
【问题描述】:

我正在尝试返回与 urns 键匹配的 uuid。

“骨灰盒”:[ “whatsapp:27820111111” ],

应该返回 d7b9f88c-b359-4d69-926d-536c0d2892e0 因为它与电话号码匹配。如何以编程方式完成。

这是两个字典。

{
"next": "https://random.com=",
"previous": null,
"results": [ 
{
        "uuid": "g2ws8dh5-b359-4d69-926d-536c0d2892e0",
        "name": "James",
        "language": "eng",
        "urns": [
            "whatsapp:27333333333"
        ],
        "groups": [
            {
                "uuid": "57fs8430-3c78-42b7-876c-c385e36c1dba",
                "name": "Postbirth WhatsApp"
            }
        ],
        "fields": {
            "whatsapp_consent": null,
            "webhook_failure_count": null,
        },
        "blocked": false,
        "stopped": false,
        "created_on": "2020-12-01T07:50:58.736502Z",
        "modified_on": "2020-12-01T08:36:34.980359Z"
    },
{
        "uuid": "d7b9f88c-b359-4d69-926d-536c0d2892e0",
        "name": "Jane",
        "language": "eng",
        "urns": [
            "whatsapp:27820111111"
        ],
        "groups": [
            {
                "uuid": "e35f2e39-3c78-42b7-876c-c385e36c1dba",
                "name": "Prebirth WhatsApp"
            }
        ],
        "fields": {
            "whatsapp_consent": null,
            "webhook_failure_count": null,
        },
        "blocked": false,
        "stopped": false,
        "created_on": "2020-12-01T07:50:58.736502Z",
        "modified_on": "2020-12-01T08:36:34.980359Z"
    }
]

}

我能够遍历并找到骨灰盒,但我不确定如何退后一步获取 uuid。

【问题讨论】:

    标签: javascript list dictionary google-apps-script nested-lists


    【解决方案1】:

    将结果数组和 urn 字符串传递给此函数。如果找到它将返回 uuid,否则将返回 null。

    function getIdFromUrn(results, urn) {
        for (let i = 0; i < results.length; i++) {
            const result = results[i];
    
            if (result.urns.includes(urn)) {
                return result.uuid; // urn found
            }
        }
    
        return null; // not found
    }
    
    console.log(getIdFromUrn(results, 'whatsapp:27820111111'));
    

    【讨论】:

    • 我测试过了。对我来说很好。你能展示你是如何将结果数组传递给函数的吗?也许你通过了整个字典。
    • 哦,是的,我通过了整个字典。
    【解决方案2】:

    当你可以使用find(...) 匹配一个值时,你不需要循环。

    请看下面的演示

    var data = [{
        "uuid": "g2ws8dh5-b359-4d69-926d-536c0d2892e0",
        "name": "James",
        "language": "eng",
        "urns": [
          "whatsapp:27333333333"
        ],
        "groups": [{
          "uuid": "57fs8430-3c78-42b7-876c-c385e36c1dba",
          "name": "Postbirth WhatsApp"
        }],
        "fields": {
          "whatsapp_consent": null,
          "webhook_failure_count": null,
        },
        "blocked": false,
        "stopped": false,
        "created_on": "2020-12-01T07:50:58.736502Z",
        "modified_on": "2020-12-01T08:36:34.980359Z"
      },
      {
        "uuid": "d7b9f88c-b359-4d69-926d-536c0d2892e0",
        "name": "Jane",
        "language": "eng",
        "urns": [
          "whatsapp:27820111111"
        ],
        "groups": [{
          "uuid": "e35f2e39-3c78-42b7-876c-c385e36c1dba",
          "name": "Prebirth WhatsApp"
        }],
        "fields": {
          "whatsapp_consent": null,
          "webhook_failure_count": null,
        },
        "blocked": false,
        "stopped": false,
        "created_on": "2020-12-01T07:50:58.736502Z",
        "modified_on": "2020-12-01T08:36:34.980359Z"
      }
    ];
    
    const lookingFor = "whatsapp:27820111111";
    
    const foundIt = data.find(el => {
      return (el.urns.indexOf(lookingFor) !== -1);
    });
    
    console.log(foundIt.uuid);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      相关资源
      最近更新 更多