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

Data Engineer

@ Lemon.io | Remote: Europe, LATAM, Canada, UK, Asia, Oceania

Artificial Intelligence – Bioinformatic Expert

@ University of Texas Medical Branch | Galveston, TX

Lead Developer (AI)

@ Cere Network | San Francisco, US

Research Engineer

@ Allora Labs | Remote

Ecosystem Manager

@ Allora Labs | Remote

Founding AI Engineer, Agents

@ Occam AI | New York