0

Can't find help on this anywhere

I have an array -

var myArray = [1,2,3]

But I need this to become -

[{"val" : 1, "checked" : false}, {"val" : 2, "checked" : false},{"val" : 3, "checked" : false}]

How is this done?

notAChance
  • 1,360
  • 4
  • 15
  • 47

2 Answers2

7

{'val' = 1, 'checked' = false} is not a valid JSON format since key and value should be separated by a colon : not =, it should be {"val" : 1, "checked" : false}

Try with the below,

var newArray = myArray.map( function(value){
   return {"val" : value, "checked" : false};
} )
Vitaliy Terziev
  • 6,429
  • 3
  • 17
  • 25
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 1
    + for proper valid JSON format use double quotes instead – Diljohn5741 Feb 16 '16 at 11:43
  • For proper json you would need the entire thing as a string literal. wouldn't it be best to handle that in the one place you actually need it to be real json, using JSON.stringify, as opposed to doing small meta programming like that all over your app? @Diljohn5741 – Dustin Stiles Feb 16 '16 at 11:47
  • This answer does work well for me, and so I'm going to accept it unless there is a better alternative? – notAChance Feb 16 '16 at 11:50
  • I think my point was missed. What I mean is, the OP didn't specify he needed JSON. When we use JSON, let's say to send an XMLHttpRequest, I always stringify it right before I send it. That would invalidate the reasoning behind every string representation that was used before it. Why not just keep a native object up until the point when you really need it to be in JSON? – Dustin Stiles Feb 16 '16 at 11:51
  • @xeon48 This is, in my opinion, the best way. Exactly the same way I would've answered it. – Dustin Stiles Feb 16 '16 at 11:51
  • I did specify JSON - in the title of the question. – notAChance Feb 16 '16 at 11:52
  • @DustinStiles Great, I'll stick with it, thanks guys. – notAChance Feb 16 '16 at 11:53
  • @DustinStiles It is still an object and not a string unless I do `JSON.stringify(newArray )`. – gurvinder372 Feb 16 '16 at 11:54
  • @gurvinder372 That's exactly my point! So what is the point behind double quotes over single quotes to, as the original commenter said, be 'valid' JSON. If it's an object until you stringify it, why worry about small meta programming like that until it matters, which is when you stringify it, replacing all single quotes with doubles anyways. – Dustin Stiles Feb 16 '16 at 11:57
  • @DustinStiles It will be a valid JSON with both single quotes and single quotes, but double quotes are more readable while scanning a JSON literal or an object. So, I would use double quotes just for better readability. – gurvinder372 Feb 16 '16 at 11:59
  • So both are acceptable when writing JSON - I.E. single and double quotes? – notAChance Feb 16 '16 at 11:59
  • @xeon48 No, when writing JSON - **NO**. But here you are returning an object which will later manifest to a JSON when you stringify the same. – gurvinder372 Feb 16 '16 at 12:01
  • 1
    Oh dear, we are confusing what JSON is. Everyone please remember that native Javascript objects had their structure long before JSON was implemented. In fact, JSON is not even a native part of the language ( not until es5 ). JSON is 'javascript object notation'. And while it can refer to the structure of a js object, it is mostly used for data, and is used as a string literal. When I say object, I mean native JS object. When I say JSON, I mean the string representation of that object. – Dustin Stiles Feb 16 '16 at 12:02
  • 1
    So JSON itself REQUIRES double quotes, as per it's spec. HOWEVER, while the object is in it's native javascript object form, double and single quotes are interchangeable. Key names do not need quotes of any sort (except reserved words) on an object, but JSON requires double quotes around EVERYTHING besides numbers, arrays, and booleans – Dustin Stiles Feb 16 '16 at 12:05
  • @DustinStiles that is correct. However, Xeon48 asked if single or double quotes are okay with JSON, which is not true. JSON doesn't accept property names in single quotes. This code will fail `JSON.parse("{'a':1, 'b':3}")` while `var a = {"val" : value, "checked" : false};` will work. But to be consistent and for readability sake, double quotes should be used while defining object itself. – gurvinder372 Feb 16 '16 at 12:05
  • 1
    It was four seconds off though so I can forgive you for not being superman :p – Dustin Stiles Feb 16 '16 at 12:06
  • 1
    And personally the type of quotes you use are a matter of personal preference. I would argue that most style guides promote the use of single quotes over double, but I haven't read enough (or any, really) to back that up. I know google, airbnb, meteor, and a few others all support single quotes over doubles. Obviously I prefer them! :D But it's all preference – Dustin Stiles Feb 16 '16 at 12:09
  • @DustinStiles Gotcha. Thanks for the info guys. – notAChance Feb 16 '16 at 15:10
2

Try this:

var myArray = [1, 2, 3];
var jsonText = [];
for (i = 0; i < myArray.length; i++) {
    jsonText[i] = {};
    jsonText[i].val = myArray[i];
    jsonText[i].checked = false;
}
JSON.stringify(jsonText);
Vitaliy Terziev
  • 6,429
  • 3
  • 17
  • 25
Sandeep
  • 712
  • 1
  • 10
  • 22