-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2633-convert-object-to-json-string.js
More file actions
29 lines (26 loc) · 1.02 KB
/
2633-convert-object-to-json-string.js
File metadata and controls
29 lines (26 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 2633. Convert Object to JSON String
* https://leetcode.com/problems/convert-object-to-json-string/
* Difficulty: Medium
*
* Given a value, return a valid JSON string of that value. The value can be a string, number,
* array, object, boolean, or null. The returned string should not include extra spaces. The
* order of keys should be the same as the order returned by Object.keys().
*
* Please solve it without using the built-in JSON.stringify method.
*/
/**
* @param {null|boolean|number|string|Array|Object} object
* @return {string}
*/
var jsonStringify = function(object) {
if (object === null) return 'null';
if (typeof object === 'boolean' || typeof object === 'number') return String(object);
if (typeof object === 'string') return `"${object}"`;
if (Array.isArray(object)) {
const elements = object.map(item => jsonStringify(item));
return `[${elements.join(',')}]`;
}
const pairs = Object.keys(object).map(key => `"${key}":${jsonStringify(object[key])}`);
return `{${pairs.join(',')}}`;
};