Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to store new object made by constructor classes data in local storage using javascript

// constructor function

function Book(title, author, pages, read = false) {
  this.title = title;
  this.author = author;
  this.pages = pages;
  this.read = read;
  this.status = function() {
    console.log(this.read);
  }
}

// array to store objects

let mybooks = [];

// instances of the Book object

let book1 = new Book('Hello', 'John', 123, true);
let book2 = new Book('Hey', 'Jane', 13, false);
let book3 = new Book('Hi', 'Mary', 12, true);
let book4 = new Book('Holo', 'Peter', 10, false);
let book5 = new Book('Banda', 'Banda', 03, true);

// push the instances to the mybooks array

mybooks.push(book1);
mybooks.push(book2);
mybooks.push(book3);
mybooks.push(book4);
mybooks.push(book5);

// store mybooks to the localStorage

localStorage.mybooks = JSON.stringify(mybooks);

// retrieve data from the localStorage

let data = JSON.parse(localStorage.getItem('mybooks'));

// call the status() method on each object

data.forEach(book => {
  book.status();
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: prevent the Confirm Form Resubmission dialog | window.history.back() confirm form resubmission 
Javascript :: Get year from user entered date in javascript 
Javascript :: disable scroll increment in react js number type 
Javascript :: Jquery works only on double click 
Javascript :: PostManDocs 
Javascript :: express dynamic api template 
Javascript :: javascript auto complete not working 
Javascript :: sort items 
Javascript :: miragejs url parameters 
Javascript :: Backbone Error: Uncaught TypeError: this.set is not a function 
Javascript :: Check If Backbone Model Has Property 
Javascript :: climbing stairs 
Javascript :: discord.js profile picture 
Javascript :: get number of new document firebasse 
Javascript :: react native componentdidmount in function 
Javascript :: anonymous function js 
Javascript :: redirect in react-router-dom v6 
Javascript :: array methods in javascript 
Javascript :: append javascript variable to html 
Javascript :: emoji picker react 
Javascript :: events js 
Javascript :: graph data structure in js 
Javascript :: how in javascript can display date and select image 
Javascript :: regex concatenazione 
Javascript :: javascript Adding Element to the Inner Array 
Javascript :: javascript for...of with Maps 
Javascript :: JavaScript HTML DOM Node Lists 
Javascript :: dropzone sending event add additional data 
Javascript :: largest element in nested array 
Javascript :: phaser place items on circle 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =