1

What I want to do is change an array, say,

[1,2,[3,4],17.5]

into a sting like

"[1,2,[3,4],17.5]"

but not like

"1,2,3,4,17.5"

i.e. keeping all of the brackets. I tried using the built in String() function, but that gave me "1,2,3,4,17.5" Is there a built-in function or a code snippet that I can use to get "[1,2,[3,4],17.5]"?
Thanks!

Sam Quinn
  • 15
  • 5
  • @MadaraUchiha This is not a duplicate because this covers all arrays including things like ["meow",7,['Q',19.36]] – Sam Quinn Jul 25 '13 at 23:44
  • 1
    It *is* a duplicate because the answer thre applies to your question. – Madara's Ghost Jul 26 '13 at 07:33
  • @MadaraUchiha: No, that is not what a duplicate _question_ means. However, even the questions are similar enough. – Lightness Races in Orbit Jul 26 '13 at 09:29
  • 1
    @LightnessRacesinOrbit: It doesn't matter what the duplicate question **means**. It matters whether or not the answer to **this** question is found in **the linked question**, which applies. The meaning of "duplicate" was changed. It's also the reason why you can't close against a duplicate with no upvoted answers. – Madara's Ghost Jul 26 '13 at 09:30
  • @MadaraUchiha: Sorry but that is simply incorrect. When we mark question A as a duplicate of question B, that shall be because question A is the same as question B. Not because one or more answers on question A happens to fit question B also. The meaning of "duplicate" has not changed. We can no long close against a dup with no upvoted answers because SE recognised that marking question A as a duplicate of question B is a waste of time if doing so won't lead the OP of question A to their answer (though [this move was _widely_ condemned](http://meta.stackexchange.com/q/165928/155739)). – Lightness Races in Orbit Jul 26 '13 at 09:31
  • Nah, it's a dupe pretty much. – Jimbo Jul 26 '13 at 09:32
  • @Jimbo: I didn't say it's not a dupe. I actually said that the questions are similar enough. I have voted to close as a dupe. I'm just saying that it's the _questions_ that matter, not the _answers_. It's called "[this question is a] duplicate of [that question]", not "[this question is not a] duplicate of [that question, but some of the answers may happen to match up!]" – Lightness Races in Orbit Jul 26 '13 at 09:33
  • Why does it matter? We've, as a community, closed this as a dupe together. This discussion is over now. – Jimbo Jul 26 '13 at 09:34
  • 1
    @rightfold: Well, that escalated quickly! You're welcome to discuss the finer things of this in the [PHP chat room](http://chat.stackoverflow.com/rooms/11/php). – Madara's Ghost Jul 26 '13 at 09:35
  • @MadaraUchiha: And you are always welcome in the [C++ Lounge](http://chat.stackoverflow.com/rooms/10/loungec)! – Lightness Races in Orbit Jul 26 '13 at 10:05
  • Final thought: it might be a good idea to mention on the duplicated question that it also applies to arrays and strings (i.e. ""Meow"") – Sam Quinn Jul 27 '13 at 14:19

3 Answers3

5

You want a JSON string:

array = [1,2,[3,4],17.5];
JSON.stringify( array );

See this question:

Convert JS object to JSON string

Does not work on IE 7 and lower!

Community
  • 1
  • 1
Tim Baas
  • 6,035
  • 5
  • 45
  • 72
  • 2
    Just add json2 for IE7 and below support https://github.com/douglascrockford/JSON-js/blob/master/json2.js – mpen Jul 25 '13 at 20:19
  • Thank you this is exactly what I needed! I missed that other question because I wasn't searching for JSON. – Sam Quinn Jul 25 '13 at 23:39
1

If you're not worried about conflicts with other pieces of code, you could change Array.prototype.toString

(function () {
    var arrayString = Array.prototype.toString;
    Array.prototype.toString = function toString() {
        return '[' + arrayString.call(this) + ']';
    }
}());

[1,2,[3,4],17.5].toString();
// "[1,2,[3,4],17.5]"
Paul S.
  • 64,864
  • 9
  • 122
  • 138
0

I'm pretty sure you could create a simple method to do this if you don't want to use json , Something along the following :

(function toBracketedString(arr) {
     if (!_.isArray(arr)) { return ""; }
     var base = "[";

     for (var element in arr){
         base.append(","+element.toString());
     }
     base.append("]");
});

Hope this helps

Pablo208
  • 23
  • 6
  • Yes I'm aware this is crappy code I just wanted to get the idea across to you – Pablo208 Jul 25 '13 at 20:30
  • I don't think this is a good example.. Someone asking a question like this, isn't helped with this sort of code.. – Tim Baas Jul 25 '13 at 20:33
  • edited to a better coding ,@Tim – Pablo208 Jul 25 '13 at 20:51
  • I didn't even look at the first version, but any snippet that contains syntax errors is a bad example already. Not to mention all the other mistakes this code has (e.g. what's the point of calling `isArray()` on `arr` if it's not an array?) – Ingo Bürk Jul 25 '13 at 20:55
  • @IngoBürk there syntax errors fixed (I did it on the fly ). I'm trying to get the idea across not do the guys coding for him – Pablo208 Jul 25 '13 at 21:09
  • @Pablo208 This would be a good alternative if it worked (I'm getting an Unexpected Identifier in V8), but on a side note, it will not work for nested arrays like ["meow",7,['Q',19.36]] – Sam Quinn Jul 25 '13 at 23:48
  • @SamQuinn sorry used of instead of in (been using coffeescript primarily as of late) , I think node may also have issues with isArray so that may be another factior – Pablo208 Jul 26 '13 at 08:42