JavaScript Assigning JSON array retrived from local storage to JavaScript array assign json array to javascript variable,assign json array to

I am storing an array of objects in localStorage, retriving it each time, attachibg the new object and again storing.

the code works fine, only if there is already an entry in localStorage Following is the code:

var companies=[];

companies=JSON.parse(localStorage.getItem("companies")); //IMP line

companies.push(company);
localStorage.setItem("companies", JSON.stringify(companies));

This works fine, when I comment the 'IMP line' for first time, so that a record is inserted with single record. once the local storage contains a record with key "companies" everything goes fine.

please suggest a condition that can be checked while inserting the array first time. Thank You.

Answer:1

You need to check:

var strCompanies = localStorage.getItem("companies")
var companies = strCompanies ? JSON.parse(strCompanies) : [];
Answer:2

So I have an HTML table populated with negative and positive values. I want to query the specific values and then upon hovering over the table values less than 0 I want to highlight the row with a red ...

So I have an HTML table populated with negative and positive values. I want to query the specific values and then upon hovering over the table values less than 0 I want to highlight the row with a red ...

url for LinkedIn advanced search: https://www.linkedin.com/vsearch/p?adv=true&trk=advsrch I am trying to fill fields and hit the submit button on the LinkedIn advanced search page using Selenium (...

url for LinkedIn advanced search: https://www.linkedin.com/vsearch/p?adv=true&trk=advsrch I am trying to fill fields and hit the submit button on the LinkedIn advanced search page using Selenium (...

  1. scrapy use selenium
  2. scrapy with selenium
  3. scrapy with selenium tutorial

i am making an app where i need 3d joystick image so that user can swipe on it and image will bend in swipped direction. For swipe down or for swipe up events what css or animation effect i should ...

i am making an app where i need 3d joystick image so that user can swipe on it and image will bend in swipped direction. For swipe down or for swipe up events what css or animation effect i should ...

Here is the select box/drop-down menu: var type_select = '<select id="type_select" style="margin-bottom:0px;">'; var i; var customer_group = <?php echo json_encode($...

Here is the select box/drop-down menu: var type_select = '<select id="type_select" style="margin-bottom:0px;">'; var i; var customer_group = <?php echo json_encode($...