sandbox-stringifier2-array
by shan10213223
JavaScript
// array
// [0, "This is a string", function(){}, "", null, undefined]
//'\'0,This is a string,function () {},,,\'' to equal
//'[0,"This is a string",null,"",null,null]'
//let input = [1,2,'a', function(){}, null, 'null'];
let input = [0,'This is a string',function () {}, , , ];
function stringifierBasics (input) {
if (input === null) {
return 'null';
}
if (input === "") {
return '""';
}
let text;
switch (typeof(input)) {
case 'number':
text = input.toString();
break;
case 'string':
text = '"' + input + '"';
break;
case 'function':
text = undefined;
break;
}
return text;
}
// collection - array
if (Array.isArray(input)) {
let outArray = [];
for (let i=0; i<input.length; i++) {
if (Object.prototype.hasOwnProperty.call(input, i)) {
console.log(input[i]);
let txt = stringifierBasics (input[i]);
//console.log(txt, typeof(txt));
outArray.push(txt);
}
}
console.log(outArray);
return '[' + outArray + ']';
}
var test = {}; // Object
test['a'] = 'test';
test['b'] = []; // Array
test['b'].push('item');
test['b'].push('item2');
test['b'].push('item3');
var json = JSON.stringify(test);
console.log(json);
//let my_details = ['John ', 31, '🤪'];
let my_details = ['🤪'];
const myJSONdetails = JSON.stringify(my_details);
console.log(myJSONdetails, typeof(myJSONdetails));
// "["John ",31,"🤪"]" --> myJSONdetails