0

I have an array of field names and an array of objects which contain all properties of fields for a module.

Given the fields1, how can i search in fields2 and take some other properties for this fields using javascript(basically i want to output fields3)?

const fields1 = ['analysis', 'casecreator'];

const fields2 = [
        {
            name: "analysis",
            label: "Analysis8D",
            mandatory: false,           
            uitype: "10",          
        },
        {
            name: "automatic_close",
            label: "Automatic Close",
            mandatory: false,             
            uitype: "56",
        },
        {
            name: "casecreator",
            label: "Case Creator",
            mandatory: false,              
            uitype: "2",
        }
]

const fields3 = [
         {
            name:"Analysis8D",
            uitype: "10"
         },
         {
            name:"Case Creator",
            uitype: "2"
         }
     ]
2
  • are you trying to match what is in fields1 to the name value of what is in fields2, then grab the uitype and match it to something in fields3 ? Commented Aug 1, 2019 at 13:23
  • @MichaelCacciano, yes exactly. Commented Aug 1, 2019 at 13:26

4 Answers 4

1

Use reduce like so:

const fields1 = ["analysis", "casecreator"];
const fields2 = [{"name":"analysis","label":"Analysis8D","mandatory":false,"uitype":"10",},{"name":"automatic_close","label":"Automatic Close","mandatory":false,"uitype":"56",},{"name":"casecreator","label":"Case Creator","mandatory":false,"uitype":"2",}];

const res = fields2.reduce((a, { name, label, uitype }) => fields1.includes(name) ? a.concat({ name: label, uitype }) : a, []);

console.log(res);

Use filter and map for a more conventional, but less efficient approach:

const fields1 = ["analysis", "casecreator"];
const fields2 = [{"name":"analysis","label":"Analysis8D","mandatory":false,"uitype":"10",},{"name":"automatic_close","label":"Automatic Close","mandatory":false,"uitype":"56",},{"name":"casecreator","label":"Case Creator","mandatory":false,"uitype":"2",}];

const res = fields2.filter(({ name }) => fields1.includes(name)).map(({ label: name, uitype }) => ({ name, uitype }));

console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

1

I know its dirty but this is to make you understand the concept.

var fields3 = []

for (let i = 0; i < fields1.length; i++) {
  for (let j = 0; j < fields2.length; j++) {
   if (fields1[i] == fields2[j].name) {
      var arry = { name: fields2[j].label, uitype: fields2[j].uitype }
      this.fields3.push(arry)
    }
  }
}

Comments

1

You can try a forEach loop like this:

    const fields1= ["analysis", "casecreator"];
    
    const fields2= [
            {
                "name": "analysis",
                "label": "Analysis8D",
                "mandatory": false,           
                "uitype": "10",          
            },
            {
                "name": "automatic_close",
                "label": "Automatic Close",
                "mandatory": false,             
                "uitype": "56",
            },
            {
                "name": "casecreator",
                "label": "Case Creator",
                "mandatory": false,              
                "uitype": "2",
            }
    ]
    
    const fields3 = [];
    
    fields1.forEach((field) => {
      
      const selectedRow = fields2.find((row) => row.name === field);
    
      if (selectedRow) {
         fields3.push({
           name: selectedRow.name,
           uitype: selectedRow.uitype
         });
       }
    
    });
    
    console.log(fields3);

I hope it will help.

Comments

1

You first need to filter the array fields2 first by checking the present of name property in fields1, and then map to create your desired objects.

const fields1 = ['analysis', 'casecreator'];

const fields2 = [{
    "name": "analysis",
    "label": "Analysis8D",
    "mandatory": false,
    "uitype": "10",
  },
  {
    "name": "automatic_close",
    "label": "Automatic Close",
    "mandatory": false,
    "uitype": "56",
  },
  {
    "name": "casecreator",
    "label": "Case Creator",
    "mandatory": false,
    "uitype": "2",
  }
]

const fields3 = fields2.filter(({
  name
}) => fields1.includes(name)).map(({
  label,
  uitype
}) => ({
  name: label,
  uitype
}))

console.log(fields3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.