Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
How can I get a specific element value of object in localstorage

I'm trying to get "city" value (in case: Porto, Lisboa, Leiria, Londres) of localstorage and put It in a variable. I wish to do this in a loop for and use each city name in a function.

Now, I'm here:

Object.entries(localStorage).map(([id, valueJSON]) => {
  cityName = JSON.parse(JSON.stringify(localStorage.getItem('persist:root', 'allWeather', 'city')));
  console.log(cityName)

My return is this:

{"allWeather":"[{"id":0.47266066302275545,"city":"Porto"},{"id":0.8669553925029849,"city":"Lisboa"},{"id":0.5502373167192611,"city":"Leiria"},{"id":0.19655712000371484,"city":"Londres"}]","_persist":"{"version":-1,"rehydrated":true}"}

How can I do this? I appreciate any help.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try this:

const cities = 
    // Parse the localstorage item to object
    JSON.parse(localStorage.getItem('persist:root', 'allWeather', 'city'))
    // Get the allWeather property
    .allWeather
    // Get the city properties only
    .map(weather => weather.city);
about 4 years ago · Juan Pablo Isaza Report

0

  • You need to convert the object string to JSON using JSON.parse

  • [{ city }] this part is not right. Change it to city

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!