April 26, 2024, 6:32 p.m. | Andrew Elans

DEV Community dev.to

I have a list of colors sitting in my database and a <select> HTML element where I want to use these colors as <option>s.





Colors


I get the values from the database and store them in a variable.



const colors = [
{val: "1", name: "Black"},
{val: "2", name: "Red"},
{val: "3", name: "Yellow"},
{val: "4", name: "Green"},
{val: "5", name: "Blue"},
{val: "6", name: "White"}
]





Generate options with Array.reduce()





With return in the reducer callback



const colorOptions = …

array colors database element html javascript list reduce store them values yellow

Founding AI Engineer, Agents

@ Occam AI | New York

AI Engineer Intern, Agents

@ Occam AI | US

AI Research Scientist

@ Vara | Berlin, Germany and Remote

Data Architect

@ University of Texas at Austin | Austin, TX

Data ETL Engineer

@ University of Texas at Austin | Austin, TX

Sr. BI Analyst

@ AkzoNobel | Pune, IN