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

84
Views
Multiply specific object in array

I have an array of objects. I want to multiply GDPAnn by 10**6.

var Annual = [{"Date":1998,"Value":4.5,"GDPAnn":9062800},{"Date":1999,"Value":4.8,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"Value":1,"GDPAnn":10581900}]

The code below returns only transformed GDPAnn. I want the similar array as above within transformed values of GDPAnn.

Annual.map((o) => o.GDPAnn * 10**6)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

For a shorter way other than making a whole function body and return statement, you can (ab)use the comma operator:

Annual.map((o) => (o.GDPAnn *= 10**6, o))

The comma operator evaluates its left operand and evaluates its right operand, then gives you the value of the right operand.

In this case, we use it to multiply o.GDPAnn and then return o after we are done.

about 4 years ago · Juan Pablo Isaza Report

0

simply change your code into this

var Annual = [
  { Date: 1998, Value: 4.5, GDPAnn: 9062800 },
  { Date: 1999, Value: 4.8, GDPAnn: 9631200 },
  { Date: 2000, Value: 4.1, GDPAnn: 10251000 },
  { Date: 2001, Value: 1, GDPAnn: 10581900 },
];

Annual.map((o) => {
  o.GDPAnn = o.GDPAnn * 10 ** 6;
  return o;
});
console.log("Annual", Annual);
// [
//   { Date: 1998, Value: 4.5, GDPAnn: 9062800000000 },
//   { Date: 1999, Value: 4.8, GDPAnn: 9631200000000 },
//   { Date: 2000, Value: 4.1, GDPAnn: 10251000000000 },
//   { Date: 2001, Value: 1, GDPAnn: 10581900000000 }
// ]
about 4 years ago · Juan Pablo Isaza Report

0

It looks like you aren't assigning the result of your map() call to anything in which case you should be using a forEach() or a simple loop to mutate the original array.

const Annual = [{ "Date": 1998, "Value": 4.5, "GDPAnn": 9062800 }, { "Date": 1999, "Value": 4.8, "GDPAnn": 9631200 }, { "Date": 2000, "Value": 4.1, "GDPAnn": 10251000 }, { "Date": 2001, "Value": 1, "GDPAnn": 10581900 }]

for (const o of Annual) {
    o.GDPAnn = o.GDPAnn * 10 ** 6
}

console.log(Annual);

If instead you actually do want to return a new array (as map() intends) then you should probably be cloning each object to avoid mutation in the original array. (Here using spread syntax (...) to clone each object and then providing the updated value for GDPAnn)

const Annual = [{ "Date": 1998, "Value": 4.5, "GDPAnn": 9062800 }, { "Date": 1999, "Value": 4.8, "GDPAnn": 9631200 }, { "Date": 2000, "Value": 4.1, "GDPAnn": 10251000 }, { "Date": 2001, "Value": 1, "GDPAnn": 10581900 }];

const updatedAnnual = Annual.map(o => ({ ...o, GDPAnn: o.GDPAnn * 10 ** 6 }));

console.log(updatedAnnual);

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!