1

I have plain string array and I want to convert it to JavaScript object basically adding a key to the values.

My array

var myArr = ["a", "b", "c"]

I want it Like

var myObj = [{"letter":"a", "letter":"b", "letter":"c"}]

Let me know the possibilities. Thank you

Krishna Kiriti
  • 83
  • 2
  • 10
  • 5
    It's impossible to have the same key 3 times in the same object. – ibrahim mahrir Oct 02 '17 at 02:09
  • Wouldn't it make more sense to just store them in an array called `letters`? Then you can either loop over them, or just access them via index? – Obsidian Age Oct 02 '17 at 02:12
  • Not possible to use the same key multiple times in an object – Big Zak Oct 02 '17 at 02:14
  • Dude, my question is how to convert them with keys not to get them into letters. Any how i tried something very basic and its working. will post it once I am sure with the results. – Krishna Kiriti Oct 02 '17 at 02:16
  • @ibrahimmahrir , myObj is array object with single key. – Krishna Kiriti Oct 02 '17 at 02:19
  • 1
    Take a close look at the object inside that array. `{ 'same key': 'a', 'same key': 'b', 'same key': 'c' }`. Notice **`same key`**. – ibrahim mahrir Oct 02 '17 at 02:20
  • You could create incrementing keys like: `{"letter_1": "a", "letter_2": "b", "letter_3": "c"}`, or build an array containing multiple objects that each have the same key... but you cannot have the same key twice in a single object. However, this sounds like an [**XY problem**](https://meta.stackexchange.com/a/66378). What exactly are you trying to achieve, and how would building an object of that (invalid) structure achieve it more efficiently than making use of a simple array? Once we know the problem you're trying to solve, we can tell you the best way of going about it. – Obsidian Age Oct 02 '17 at 02:23
  • @ObsidianAge incrementing keys => sould use arrays. – ibrahim mahrir Oct 02 '17 at 02:24

2 Answers2

2

This is how I would implement that. You can probably make it less verbose by not even making the var obj, but for the purpose of illustration I have written it as follows:

function strings_to_object(array) {

  // Initialize new empty array
  var objects = [];


  // Loop through the array
  for (var i = 0; i < array.length; i++) {

    // Create the object in the format you want
    var obj = {"letter" : array[i]};

    // Add it to the array
    objects.push(obj);
  }

  // Return the new array
  return objects;
}

Output:

[ { letter: 'a' }, { letter: 'b' }, { letter: 'c' } ]

I realize the output is slightly different in that your output is a single array with one objet. The problem there is that the object has several repeating keys. Just like in your original array (of size 3), your output array should be of size 3. Each object will have a "letter" key.

1

Don't assign each letter to a duplicate letter key (which really doesn't exist or make sense). Instead, use your array by storing each element in a separate item slot in the array, each having a type and a value:

[
    {
        type: "letter",
        value: "a"
    },
    {
        type: "letter",
        value: "b"
    },
    {
        type: "letter",
        value: "c"
    }
]

Or, as Obsidian Age commented, an array for letters (and then other arrays for other types of characters):

var letters = ['a', 'b', 'c'];

There's no one right way to store this data.

clabe45
  • 2,354
  • 16
  • 27