Answers for "A class I made for creating OOP styled JSON objects"

0

A class I made for creating OOP styled JSON objects

class JSON_Instance{
    // constructor(name, age){
    //   this.name = name;
    //   this.age = age;
    // }
    
    JSONobj = {
      innerArray:[]
    };
    
    insertJSON(str){
      this.JSONobj = str;
      console.log(this.JSONobj)
    }
  
    addToObj(passedArray){    
      try{
        var tmpObj = Object.fromEntries(passedArray);
        this.JSONobj.innerArray.push(tmpObj);
  
      }
      catch(error){
        console.error("Parameter must be a 2D Array")
      }
    }
    
    removeByIndex(indexToRemove){
      this.JSONobj.innerArray.splice(indexToRemove, 1);
    }
    
    saveToLocalStorage(name){
      localStorage.setItem(name, this.stringMe());
    }
    
    pulled = "";
    getFromLocalStorage(name, insertToJSON){
      if(insertToJSON==true){
        this.pulled = localStorage.getItem(name);
        this.JSONobj = this.pulled;
        this.parseMe();
      }else{
        this.pulled = localStorage.getItem(name);  
      }
      
      return this.pulled;
    }
    
    print(){
      console.log(this.JSONobj);
      return this.JSONobj;
  
    }
  
    stringMe(){
      return JSON.stringify(this.JSONobj);
    }
  
    parseMe(){
      this.JSONobj = JSON.parse(this.JSONobj);
      console.log("Parsed!");
      console.log(this.JSONobj);
    }
  
  }
  
//My GIThub
//https://github.com/pixman92/JSON_Instance
Posted by: Guest on October-10-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language