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

224
Views
calculate JSON.stringify(obj) to sum value of string?

In my local storage I have some score value stored in array

var obj = localStorage.getItem("scoreList")

output

[0,10,10,0,10]

I want sum of this value like and return to data value

sum = 30

I have tried to convert into string value

var string = JSON.stringify(obj);
output "[0,10,10,0,10]"

How can I execute this sum value ?

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

0

[0,10,10,0,10].reduce((sum, a) => sum + a, 0);

about 4 years ago · Juan Pablo Isaza Report

0

localStorage.getItem("scoreList").replace(/(\[|\])/g, '').split(",").map(x => parseFloat(x)).reduce((a,b) => a+b, 0)

a simple way:

JSON.parse(localStorage.getItem("scoreList")).reduce((x, y) => x + y)
about 4 years ago · Juan Pablo Isaza Report

0

If you set the scoreList value as an array, that is:

localStorage.setItem('scoreList', [0,10,10,0,10])

When you get it back it will still be a string, so there's no need for any other kind of validation or data type transformation as you did with JSON.stringfy. The following shoud be enough:

const obj = localStorage.getItem("scoreList")
const total = obj.reduce((sum, item) => sum + item, 0);
//total = 30
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!