Search code examples
javascriptstringify

How to use recursion so this function will work with nested arrays and objects


I'm attempting to write a JSON.stringify function. It works well enough for basic arrays and objects but how can I get it to work for nested arrays?

function stringifier (input) {
  
 
  let output = '';
  
  if (typeof input === 'number'){
   output+= input
 }else if (input === undefined){
   output = undefined
 }else if (input === null){
 output += 'null'
 }else if (typeof input === 'string'){
   output += `"${input}"`
 }else if(typeof input === 'function'){
 output = undefined
 }
 
 else if (Array.isArray(input)){
   output+='['
   for (let i =0 ; i < input.length ; i++){
     if (typeof input[i] === 'string'){
     output += `"${input[i]}",`
     }else if(typeof input[i]=== 'number'){
       output += `${input[i]},`
     }else if(input[i] === null || typeof input[i] === 'function' || input[i]=== undefined){
       output += 'null,'
     }
   }output = output.substring(0,output.length-1)
   output += ']'}
 
 else if(Array.isArray(input)=== false && typeof input === 'object'){
 output+='{'
 for (let [key, value] of Object.entries(input)) {
   
   if (typeof value === 'string'){
  output += `"${key}":"${value}",`
   }else if (typeof value === 'number'){
     output += `"${key}":${value},`
   }else if (value === null){
     output += `"${key}":null,`
   }
   
 }output = output.substring(0,output.length-1) 
 output += '}';}

 return output
}

I'm not sure how to use recursion so that it loops through the function again for nested arrays. please help. Am I far away?


Solution

  • You're actually so close!!
    So where you have your Array.isArray(input)=== false && typeof input === 'object' block (which is essentially: "Do I have a JSON object?") You can call the recursion there:

    EDIT: It's the same with for nested Arrays, just run stringifier on the result of that!

    function stringifier(input) {
    
      let output = '';
    
      if (typeof input === 'number') {
        output += input
      } else if (input === undefined) {
        output = undefined
      } else if (input === null) {
        output += 'null'
      } else if (typeof input === 'string') {
        output += `"${input}"`
      } else if (typeof input === 'function') {
        output = undefined
      } else if (Array.isArray(input)) {
        output += '['
        for (let i = 0; i < input.length; i++) {
          output += `${stringifier(input[i])},`;
        }
        output = output.substring(0, output.length - 1)
        output += ']'
      } else if (Array.isArray(input) === false && typeof input === 'object') {
        output += '{'
        for (let [key, value] of Object.entries(input)) {
          output += `"${key}":"${stringifier(value)}",`
        }
        output = output.substring(0, output.length - 1)
        output += '}';
      }
    
      return output
    }
    
    
    var result = stringifier({
      a: 123,
      arr: [1, 2, 3],
      nested: [[11, 12], [21, 22]],
      b: {
        another: 456,
        key2: "something"
      }
    })
    console.log(result)

    Perhaps adding in indentation to help prettify it might be a nice touch for later on?