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

How do I create object from multiple arrays?

So let’s say I have 2 arrays

keyword = ['test','123']
lang = ['en','fr']

From these two, I want to create an object from keyword that contains each lang. Like below

keywords = [
    {
      keyword: "test",
      lang: "en",
    },
    {
      keyword: "test",
      lang: "fr",
    },
    {
      keyword: "123",
      lang: "en",
    },
    {
      keyword: "123",
      lang: "fr",
    },
  ];

So in total I’ll have 4 objects cause each keyword will have their own lang. I’ve tried mapping but it’s just not possible once I have 1 keyword and 2 languages.

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 :

use a nested loop:

const keywords = [];
for (const k of keyword) {
  for (l of lang) {
    keywords.push({ keyword: k, lang: l });
  }
}
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