0

I have an array of strings. I want to convert it to an array of objects.

For example:

var x =['one','two','three'];

Is there way to convert it array of objects?

dibi
  • 3,257
  • 4
  • 24
  • 31
brk
  • 48,835
  • 10
  • 56
  • 78

2 Answers2

0

This is what I came up with.

   function toObject(arr) {
      var rv = {};
      for (var i = 0; i < arr.length; ++i)
        if (arr[i] !== undefined) rv[i] = arr[i];
      return rv;
    }
Adi
  • 5,560
  • 1
  • 24
  • 37
0

This returns an object with a property strings which is the array you pass in

function convertArrayToObject (array) {
  var obj = {
    "strings": array
  }
  return obj
}
Mascros
  • 341
  • 1
  • 3
  • 12