Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stringify({ data: { hello: 'world', with: { nested: 'objects' } } })
- {
- "data": {
- "hello": "world",
- "with": {
- "nested": "objects"
- }
- }
- }
- function stringify(object, indentation) {
- if (typeof indentation === 'undefined') {
- indentation = 0;
- }
- var items = [];
- for (var key in object) {
- if (object.hasOwnProperty(key)) {
- var value;
- switch (typeof object[key]) {
- case 'object':
- var type = Object.prototype.toString.call(object[key]);
- switch (type) {
- case '[object Null]':
- value = 'null';
- break;
- case '[object Array]':
- case '[object Object]':
- value = stringify(object[key], indentation + 2);
- break;
- default:
- value = type;
- }
- break;
- case 'string':
- value = '"' + object[key] + '"'
- break;
- default:
- value = object[key];
- }
- items.push(Array(indentation + 3).join(' ') + '"' + key + '": ' + value);
- }
- }
- return '{n'
- + items.join(',n') + 'n'
- + Array(indentation + 1).join(' ') + '}';
- }
- // Example:
- console.log(stringify({ data: { hello: 'world', with: { nested: 'objects' } } }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement