1

I'm trying to loop through an array object with JavaScript forEach, then the function should create an OPTION element, set its value to the item's id, set its text to the item's name, and then add the OPTION to the SELECT element?

I've tried returning each item as template literals

const currencies = [{
 id: 'USD', name: 'US Dollars'
 }, { 
 id: 'UGX', name: 'Ugandan Shillings'
 }, {
 id: 'KES', name: 'Kenyan Shillings'
 }, {
 id: 'GHS', name: 'Ghanian Cedi'
 }, {
 id: 'ZAR', name: 'South African Rand'
 }];
currencies.forEach(function(currency){ 
 str = `<option value="${id}"></option>` 
}) 
Amadan
200k23 gold badges254 silver badges321 bronze badges
asked Feb 13, 2019 at 5:01

5 Answers 5

2

You can do something like this:

const currencies = [{
 id: 'USD',
 name: 'US Dollars'
}, {
 id: 'UGX',
 name: 'Ugandan Shillings'
}, {
 id: 'KES',
 name: 'Kenyan Shillings'
}, {
 id: 'GHS',
 name: 'Ghanian Cedi'
}, {
 id: 'ZAR',
 name: 'South African Rand'
}];
const selectEl = document.querySelector('#selectEl');
currencies.forEach(function(currency) {
 const option = document.createElement('option');
 option.setAttribute('value', currency.id);
 option.text = currency.name;
 selectEl.appendChild(option);
})
<select id="selectEl">

answered Feb 13, 2019 at 5:09
Sign up to request clarification or add additional context in comments.

Comments

0

const currencies = [{ id: 'USD', name: 'US Dollars' }, { id: 'UGX', name: 'Ugandan Shillings' }, { id: 'KES', name: 'Kenyan Shillings' }, { id: 'GHS', name: 'Ghanian Cedi' }, { id: 'ZAR', name: 'South African Rand' }];
$('select').append(currencies.map(c => $(`<option value="${c.id}">${c.name}</option>`)));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select></select>

answered Feb 13, 2019 at 5:05

Comments

0

You have to use Template literals (that are enclosed with back-tick (` `)) which allows embedded expressions:

const currencies = [{ id: 'USD', name: 'US Dollars' }, {
id: 'UGX', name: 'Ugandan Shillings' }, { id: 'KES', name: 'Kenyan Shillings' }, { id: 'GHS', name: 'Ghanian Cedi' }, { id: 'ZAR', name: 'South African Rand' }];
var str = '';
currencies.forEach(function(currency){
 str += `<option value=${currency.id}>${currency.name}</option>`;
});
document.getElementById('mySelect').innerHTML = str;
<select id="mySelect"></select>

answered Feb 13, 2019 at 5:07

Comments

0

Here you have one approach thay uses Array::map() and Array::join():

const currencies = [
 {id: 'USD', name: 'US Dollars'},
 {id: 'UGX', name: 'Ugandan Shillings'},
 {id: 'KES', name: 'Kenyan Shillings'},
 {id: 'GHS', name: 'Ghanian Cedi'},
 {id: 'ZAR', name: 'South African Rand'}
];
let sel = document.getElementById("mySelect");
sel.innerHTML = currencies.map(
 ({id, name}) => `<option value=${id}>${name}</option>`
).join("");
<select id="mySelect"></select>

answered Feb 13, 2019 at 5:11

Comments

0

With jQuery

const currencies = [{ id: 'USD', name: 'US Dollars' }, {
id: 'UGX', name: 'Ugandan Shillings' }, { id: 'KES', name: 'Kenyan Shillings' }, { id: 'GHS', name: 'Ghanian Cedi' }, { id: 'ZAR', name: 'South African Rand' }];
currencies.forEach(function(currency){
 $('#list').append(`<option value="${currency.id}">${currency.name}</option>`)
})
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<select id="list"></select>

With pure JS

const currencies = [{ id: 'USD', name: 'US Dollars' }, {
id: 'UGX', name: 'Ugandan Shillings' }, { id: 'KES', name: 'Kenyan Shillings' }, { id: 'GHS', name: 'Ghanian Cedi' }, { id: 'ZAR', name: 'South African Rand' }];
currencies.forEach(function(currency){
 var el = document.getElementById('list');
 var elChild = document.createElement('option');
 // Give the new div some content
 elChild.innerHTML = currency.name;
 elChild.id = currency.id
 // Jug it into the parent element
 el.appendChild(elChild);
})
<select id="list"></select>

answered Feb 13, 2019 at 5:04

4 Comments

How do I do this without jQuery?
I have added a snippet with pure JS.
You need to be setting value, not id
The value is set by innerHTML

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.