-3

I have the array: ["1", "2", "3", "4"]. I would like to transform this into an object like this: {"id": "1", "id": "2", "id": "3", "id": "4"}.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mihai Ilie
  • 29
  • 4
  • Refer - http://stackoverflow.com/questions/2295496/convert-array-to-json – captainsac May 06 '15 at 12:44
  • 3
    A Javascript object cannot have multiple separate properties with the same name, therefore what you want to do is not possible. What are you _actually_ trying to do? – Etheryte May 06 '15 at 12:45
  • You want to parse an array into a JSON object. See this http://stackoverflow.com/questions/2295496/convert-array-to-json – La masse May 06 '15 at 12:46
  • 2
    **AM I GOING MAD HERE???** 3 upvotes for something very poorly explained, and 3 answers that have assumed completely different questions? There is **NO** mention of JSON, and **NO** mention of multiple objects in an array – musefan May 06 '15 at 12:50
  • this may give you a better idea of what you can and cannot do ;) http://eloquentjavascript.net/04_data.html – Vitaliy Terziev May 06 '15 at 13:37

2 Answers2

1

It is not possible to have a single object with the same property many times. We understand you want a array of objects with id property, so, you could loop between this array and for each item add a new object into a new array, for sample:

var data = ["1", "2", "3", "4"];

var result = []; 

for(var i = 0; i < data.length; i++) { 
   result.push({ id: data[i] }); 
}

console.log(result); // [ { id: "1" }, { id: "2" }, { id: "3" }, { id: "4" }]
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • Yeah that was my best guess at what OP wanted... but that is certainly not what they have asked for. There is only 1 single object in the example – musefan May 06 '15 at 12:48
  • OK, you get my downvote, If you can justify why this is an appropriate answer then I will retract my vote – musefan May 06 '15 at 13:00
  • Ok @musefan, I tried to explain what we understand the OP would like to do. I explained that it wasn't possible to obtain an object with the same properties and give an possible solution. The OP does not anwser the comments bellow the question, so, we supose the solution. I hope it helps him :) – Felipe Oriani May 06 '15 at 13:05
  • But why help if the OP doesn't bother to help themselves? If OP can't answer a simple question about what they want, then why try to give them a solution? What happens if you guess right? OP adds it to there project, understand nothing, and then just builds crap upon crap. If a programmer can't understand what they are doing then they shouldn't do it until they learn, all your helping to do is make them a bad programmer and that doesn't help anyone – musefan May 06 '15 at 13:10
-1

You could do:

JSON.stringify(["1", "2", "3", "4"])

but that would only result in:

   { ["1","2","3","4"] }

You would have to build an object from your array and then stringify it. If you want an example, I can provide it.

EDIT: Posted the same time as Felipe Oriani above, his solution is what I am referring to.

Evidica
  • 1,464
  • 1
  • 14
  • 19
  • Why do you get the impression the OP wants anything to do with JSON? – musefan May 06 '15 at 12:56
  • Just because his output looked like JSON, don't get so salty man, jeez. – Evidica May 06 '15 at 12:59
  • You object to people asking you questions? You must be great fun to talk to.... you could argue the input looks like JSON too, but without specific clarification from the OP I don't think people should attempt to answer the question – musefan May 06 '15 at 13:02
  • Not at all but the question you asked was pointless, if I assumed JSON it's for obvious reasons based on the output that OP posted. OP didn't give us a whole lot to work with either. Instead of acting like OP isn't good enough, I was just trying to solve his problem as best as I could see. Sorry it hurt your feels man. – Evidica May 06 '15 at 13:05
  • Not pointless, because you may have noticed something I was unable to see, but it seems not. You should ask for clarification from the OP and when (if) they provide it then you can answer correctly rather than just guessing... that would be logical and sensible, no? – musefan May 06 '15 at 13:07
  • If OP cared enough to have his problem solved, he would ask the proper question and provide details. He gave us a half prepared question so he can't expect much in return from the community. Flame him, not the people trying to help for free. – Evidica May 06 '15 at 13:08
  • yeah, if OP don't care then don't help. All you do by answering is encourage them to ask poor questions next time. You gotta give people some motivation to do something, force them to ask better questions and they will ask better questions from then on – musefan May 06 '15 at 13:13