0

Suppose there is a select menu like this:"

<select id="dropDown"></select>

And I wish to populate it with an array value ["apple","mango","banana"];
How can this be achieved at one go without using any looping construct?

5
  • 1
    without using any looping construct. Why? Commented Apr 23, 2014 at 7:23
  • 1
    Is there a reason why you are avoiding using a loop to do this? Commented Apr 23, 2014 at 7:23
  • 1
    Is this even possible? Wouldn't all solutions at least have some implied "looping construct" under the hood? Commented Apr 23, 2014 at 7:24
  • @Zhihao: hardcode array indexes. select.add(new option(array[0])); select.add(new option(array[1])); select.add(new option(array[2])); Commented Apr 23, 2014 at 7:26
  • @Cerbrus Ah, of course. I hadn't even considered it, but lets hope it doesn't come to that. :) Commented Apr 23, 2014 at 7:27

4 Answers 4

3

Here you can do like

var arr = ["apple", "mango", "banana"];
document.getElementById('dropdown').innerHTML =
    '<option>' + arr.join('</option><option>') + '</option>';

JSFiddle

Sign up to request clarification or add additional context in comments.

3 Comments

.join() still uses a loop under the hood.
would there be any difference,while using very large arrays?(using/not using foreach or any loop)
@curiousUser first understand about the need of looping.. inorder to prevent code redundancy, so there will same performance in your case..so you can use any no. Of values in the array
3

Without the use of a loop, you would do something like this:

var x = document.getElementById("dropDown");
var option = document.createElement("option");
option.innerHTML = "apple";
x.appendChild(option);

option = document.createElement("option");
option.innerHTML = "mango";
x.appendChild(option);

option = document.createElement("option");
option.innerHTML = "banana";
x.appendChild(option);

Obviously this assumes you know what the array values are going to be ahead of time. The most common way of doing this however would be to use a loop to iterate over the array.

1 Comment

+1 since this is the only answer that uses no loops whatsoever. Just fix the .add() and .text syntax, please, since that's not quite how DOM manipulation works (You can find the proper functions in my answer)
1

If you just mean not using looping construct like for/while:

document.getElementById('dropDown').innerHTML = ["apple","mango","banana"].map(function(e) {
  return "<option>"+e+"</option>";
}).join('');

The demo.

2 Comments

.map() and .join() still use loops under the hood.
@Cerbrus That's true and that's what i said at first, looping construct.
0

You'll want to do this:

var options = ["apple","mango","banana"],
    select = document.getElementById('dropDown');

for(var i = 0; i < options.length; i++){
    var option = document.createElement("option");
    option.value = option.innerHTML = options[i];
    select.appendChild(option);
}

The "No loops" requirement is really a bad idea. This is the most efficient way to build the list, in native JS. (Aside from micro optimizations in the loop)

It also doesn't use innerHTML on DOM elements that are rendered already, avoiding the need for the browser to re-structure the entire DOM.

1 Comment

Could the person that downvoted my answer please explain why? If I made a mistake, I'd like to learn from it. If it's because of the "loop" requirement: Keep in mind that a OP's requirements aren't always the best way to solve the issue, and most of the other answers are also using loops, disguised as other functions.

Your Answer

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.