【问题标题】:Loop the JSON String using filter function by passing array values, Javascript通过传递数组值,Javascript,使用过滤器函数循环 JSON 字符串
【发布时间】:2022-11-16 22:43:57
【问题描述】:
[
  {
    "dataType": "BOOLEAN",
    "name": "checkbox_confidential",
    "isCollection": false,
    "flowName": "FlowData",
    "value": true,
    "objectType": null
  },
  {
    "dataType": "BOOLEAN",
    "name": "checkbox_password",
    "isCollection": false,
    "flowName": "FlowData",
    "value": false,
    "objectType": null
  },
  {
    "dataType": "BOOLEAN",
    "name": "checkbox_restriction",
    "isCollection": false,
    "flowName": "FlowData",
    "value": true,
    "objectType": null
  },
  {
    "dataType": "DATEONLY",
    "name": "date_dateReq",
    "isCollection": false,
    "flowName": "FlowData",
    "value": "2022-10-30",
    "objectType": null
  }]

我正在从流向 LWC 的流程中获取上述 JSON。 我将上面的 Json 从 event.detail 捕获到一个名为 outputVariables 的变量;

let { outputVariables, status } = event.detail;
_searchOutputVariables = ['checkbox_restriction','date_dateReq'];

const result = outputVariables.filter(outvar => outvar.name == "checkbox_restriction");
  if(result != undefined && result.length > 0){
     this.restrictionLocal = result[0].value;
  }

我试图通过像上面那样传递每个变量来获取值。但是我的领导要我创建一个数组并添加所有过滤文本并传递给过滤函数。我创建了一个数组“_searchOutputVariables”。如何在过滤函数中使用该数组?请帮我做这个?



【问题讨论】:

    标签: javascript salesforce lwc


    【解决方案1】:

    这很简单:

    const outputVariables = [
      {
        "dataType": "BOOLEAN",
        "name": "checkbox_confidential",
        "isCollection": false,
        "flowName": "FlowData",
        "value": true,
        "objectType": null
      },
      {
        "dataType": "BOOLEAN",
        "name": "checkbox_password",
        "isCollection": false,
        "flowName": "FlowData",
        "value": false,
        "objectType": null
      },
      {
        "dataType": "BOOLEAN",
        "name": "checkbox_restriction",
        "isCollection": false,
        "flowName": "FlowData",
        "value": true,
        "objectType": null
      },
      {
        "dataType": "DATEONLY",
        "name": "date_dateReq",
        "isCollection": false,
        "flowName": "FlowData",
        "value": "2022-10-30",
        "objectType": null
       }
     ];
     
    const _searchOutputVariables = ["checkbox_restriction", "date_dateReq"];
    
    const result = outputVariables.filter(node => {
      if (_searchOutputVariables.includes(node.name))
        return node;
    });
    
    console.log(result);

    【讨论】:

    • 嗨托马斯,非常感谢你的帮助。我刚开始学习 Javascript。还有一个问题,如何通过传递名称数组来仅获取值?
    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 2015-03-24
    • 2023-01-27
    • 2017-03-21
    • 1970-01-01
    • 2014-11-20
    • 2016-10-26
    相关资源
    最近更新 更多