0

Exists a more elegant way to create an object from an array than this one?

var createObject = function(){
var myArray= generateArray();
var myObject = {
    question : myArray[0],
    answerA : myArray[1],
    answerB : myArray[2],
    answerC : myArray[3],
    answerD : myArray[4]
    };
return myObject;
}
1
  • It would be more elegant with better indenting. Anyway...if array element 0 is always the question and all remaining elements become answers A, B, C, etc., then you could use a loop - but if there are only ever four answers the loop code isn't going to be much shorter, so... Commented Sep 28, 2015 at 0:54

4 Answers 4

1

What's your background? Python?

 var values = generateArray();
 var obj = {};
 ["question", "answerA", "answerB", "answerC", "answerD"].forEach(function(item, idx) {
     obj[item] = values[idx];
 });

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

5 Comments

Considering the tags are all Javascript tags, and that he's using function(){...} to declare his function, my guess is he's using Javascript. ;-)
@mbinette I know, I asked for the background ;) It seams like he is not used to js, but surely understand programming..
@mbinette I specially asked about python, because it provides you some really nice features which can solve such problems in one line..
But it doesn't answer the question, which is "Convert an array into an object in JavaScript" (see title). I love Python, don't get me wrong, but it's not Javascript.
Love the functional style of this solution and yes, I'm also doing Python.
0

You could define an attribute map:

var createObject = function(){
    // The map should have the attribute names set in order defined in `myArray`
    var map = ["question", "answerA", "answerB", "answerC", "answerD"]
    var myArray = generateArray();
    var myObject = {};
    for(var i in map)
        myObject[map[i]] = myArray[i];
    return myObject;
}

Comments

0

Try this to go beyond 'A', 'B', 'C', 'D':

  function arrayToObject ( array ) {
    return array.reduce(
      function ( object, cell, index ) {
        if (index > 0) object['answer' + String.fromCharCode(64 + index)] = cell;
        else           object['question'] = cell;
        return object;
      }, {}
    );
  }

Comments

0

If you would like to have a function that takes an array as an argument, and returns an object, this is how I would do it:

var toObject = function(arr) {
  var newObj = {};

  for(var i = 0; i < arr.length; i++) {
    newObj[i] = arr[i];
  }
  return newObj;
};

However, keep in mind that Javascript arrays are simply objects with more methods available to them such as push(), pop(), and the length property.

Hope this helps!

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.