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

running data given by a dictionary (javascript)

How do I run the data in the dictionary?

input:

const items = {
  34: ["a", "t"],
  43: ["d", "u"],
};

output:

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

run({34: "a"}),
run({34: "t"}),
run({43: "d"}),
run({43: "u"}),

Tried this:

[{ [Object.keys(items)[0]]: items[Object.keys(items)[0]] }]

but the output isn’t quite what I wanted.

>Solution :

  1. Iterate the entries then iterate the value array.
  2. Construct an object from the entry key and the array value and pass it to run()
const items = {
  34: ["a", "t"],
  43: ["d", "u"],
};

// for demonstration purposes
const run = (arg) => console.log("run:", JSON.stringify(arg));

Object.entries(items).forEach(([ key, arr ]) => {
  arr.forEach((val) => {
    run({ [key]: val });
  });
});
.as-console-wrapper { max-height: 100% !important; }
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