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

Creating an object from two arrays

How to create an object from the below arrays

var v=[1,2,3,4,5,6]

var v1=['A','B','C','D','E']

var result={};

key’s for the object are (name & target). I want to map the keys with the above two arrays as value like below

 {name: "1", target: "A"}
 {name: "2", target: "B"}
 {name: "4", target: "C"}

{name: "5", target: "D"}
{name: "6", target: "E"}

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

I was trying with the below logic

v.forEach(key=> result[key]= key);
console.log('ColResult',result);

>Solution :

If the first array consists of increasing numbers equal to the index then there is no need for that array at all…

result = [];
v1.forEach((v, i) => result.push({ name: i, target: v1[i] }));

but in general it should be something like

result = [];
for (var i = 0; i < v.length; i++) {
  result.push({ name: v[i], target: v1[i] });
}
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