0

I have created method that take certain data from an array, and creates a new dictionary with the array by adding in some pre-defined strings,

Im able to create the new array's I need, but I seem to be storing it as a String variable instead of as an array. Here is my Code:

var createContestantsDropdownlist = ContestantInfoDropdown();

function ContestantInfoDropdown() {
var ContestantTest = ['Contestant 1', 'Contestant 2', 'Contestant 3', 'Contestant 4'];  
var option = [];
    for (var i=0;i<ContestantTest.length;i++){
    option += '{value: \'' + ContestantTest[i] + '\'},';
    }
    option += '{value: \'Contestant 5\'}';
    return option
  };

When I take a look at the data that has been created, it's all in the correct format, but I need to try and remove the double quotes at the beginning and end to make it an array, and not a string value. See here how the string value Looks.

All I would like to know is how to convert this from a string by simply removing the Double quotes at the beginning and the end, or am I completely on the wrong track here. any help appreciated!

Thanks!

5
  • The double quotes are not part of the string. It's just the way the browser console shows you that it is a string. Commented Mar 30, 2016 at 18:02
  • If you want to make it an array, append objects to the string. You're turning it into a string by using += with those strings you're building. Commented Mar 30, 2016 at 18:02
  • Well you're concatenating strings in your option variable, so it's a string. Commented Mar 30, 2016 at 18:03
  • using '+=' on 'options' variable cast it to a string. Instead use option.push(ContestantTest[i]) Commented Mar 30, 2016 at 18:03
  • Are you looking for an array of objects? Commented Mar 30, 2016 at 18:03

4 Answers 4

2

Try this:

option.push({value: ContestantTest[i]});

That will actually push the object onto the array rather than build out a string like you are now.

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

Comments

0

Instead of concatenating a string you should push the values onto the array like:

function ContestantInfoDropdown() {
  var ContestantTest = ['Contestant 1', 'Contestant 2', 'Contestant 3', 'Contestant 4'];  
  var option = [];
      for (var i=0;i<ContestantTest.length;i++){
          option.push({value: ContestantTest[i] });
      }
      option.push({value: 'Contestant 5' });
      return option
};

var createContestantsDropdownlist = ContestantInfoDropdown();

console.log(createContestantsDropdownlist);

example fiddle https://jsfiddle.net/c9j1hure/

1 Comment

Thank you very much for your Help!! Worked straight away.
0

using += on options variable cast it to a string. Use options.push(ContestantTest[i]) instead.

Comments

0
var createContestantsDropdownlist = ContestantInfoDropdown

function ContestantInfoDropdown()
{
    var ContestantTest = ['Contestant 1', 'Contestant 2', 'Contestant 3', 'Contestant 4']
    var option = ContestantTest.map(function(x) { return { value: x } })
    option.push ({value: 'Contestant 5'})
    return option
}

Comments

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.