-1

In an answer for how to declare 2D JavaScript arrays, Declare an empty two-dimensional array in Javascript? Kamil Kiełczewski utilized the method JSON.stringify.replace() to format the array output and passed an expression (regular expression?) that I did not understand. I checked the spec at https://tc39.es/ecma262/#sec-json.stringify which does not mention a .replace() method. I am trying to locate the documentation for the .replace() method and would like to understand the expressions that the JSON.stringify.replace() method expects.

The only other reference besides Kamil's was in a description to safely convert JSON strings into JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

yamex5
  • 195
  • 1
  • 11
  • 1
    The code in question calls `.stringify()`, which returns a string, and then calls `.replace()`, a string method, on that. – Pointy Apr 30 '20 at 19:11
  • 5
    `JSON.stringify.replace` is not a function. However, `JSON.stringify` returns a string, and strings have a `.replace` method which is what you're looking for. [It's docs are here.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) – CRice Apr 30 '20 at 19:12
  • 1
    To be clear, there is no such thing as `JSON.stringify.replace` and the code you cited does not involve that. – Pointy Apr 30 '20 at 19:12
  • @Crice I can't delete this question, can you please answer it so I can close it? – yamex5 Apr 30 '20 at 20:01
  • 1
    @yamex5 An answer already exists that looks good to me, you can accept that one. Moreover you can always post and accept an answer to your own question if you figure out the solution yourself. – CRice Apr 30 '20 at 22:34

1 Answers1

2

Notice the invocation of stringify to return a string, so you can call replace on that string...

JSON.stringify({ x: 5, y: 6 }).replace()

Documentation on String.prototype.replace method

Taylor Burke
  • 424
  • 4
  • 15