Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop the JSON String using filter function by passing array values, Javascript

[
  {
    "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
  }]

I am getting the above JSON from the flow to LWC.
I am capturing the above Json to a variable called outputVariables from event.detail;

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;
  }

I am trying to get the value by passing each variable like the above. But my lead wants me to create an array and add all the filter text and pass to the filter function. I create an array ‘_searchOutputVariables’. how to use that array in filter function? please help me to do this?



MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It is pretty easy:

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading