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

139
Views
How do i update all values of an object in an array?

How do I change all salary values and increase them by a percentage, when each salary is a property of an object, with each object being stored in an array? E.g. increasing by 10 percent and I have to round the result up to the nearest integer:

raiseSalary([
    { name: "Ali", salary: 3000 },
    { name: "Rob", salary: 2000 },
    { name: "Adam", salary: 4500 },
], 10)

The above call should return:

[
   { name: 'Ali', salary: 3300 },
   { name: 'Rob', salary: 2200 }, 
   { name: 'Adam', salary: 4950 }
]

This is the code that I have written:

function raiseSalary(arr, raise) {
    if (arr.length === 0) {
        return [{}];
    } else {
        const raiseArray = arr.map((salaryObj) => {
            return (salaryObj.salaryObj / 100) * 10;
        });
    }
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The map method should do what you want. Note the destructuring of salaryObj to avoid manipulating the original object.

const percent = 10;
const multiplier = 1 + (percent / 100);
const salaries = [
  { name: "Ali", salary: 3000 },
  { name: "Rob", salary: 2000 },
  { name: "Adam", salary: 4500 },
];
const newSalaries = salaries.map((salaryObj) => {
  return {
    ...salaryObj,
    salary: salaryObj.salary * multiplier,
  };
});

console.log(newSalaries);
about 4 years ago · Juan Pablo Isaza Report

0

Presented below is one possible way to achieve the desired objective.

Code Snippet

const addRaise = (arr, pcnt) => (
  arr.map(
    ({ salary, ...rest }) => ({
      ...rest,
      salary: ( +salary * (+pcnt + 100) / 100 )
    })
  )
);

/*
// method to raise salary by "pcnt" percent
const addRaise = (arr, pcnt) => (
  // iterate over array "arr"
  arr.map(
    // destructure to access "salary"
    ({ salary, ...rest }) => ({
      ...rest,        // all other props retained as-is
      // salary updated to be 10% more than current
      salary: ( +salary * (+pcnt + 100) / 100 )
    })
  )
);
*/

const dataArr = [{
    name: "Ali",
    salary: 3000
  },
  {
    name: "Rob",
    salary: 2000
  },
  {
    name: "Adam",
    salary: 4500
  },
];

console.log(
  'adding raise of 10%...\n',
  'new array:\n',
  addRaise(dataArr, 10)
);

Explanation

Comments added to the snippet above.

about 4 years ago · Juan Pablo Isaza Report

0

You're almost there. Just a few points

  • Just as you return something when the array has zero elements, you have to return something when it has elements; in this case the modified array
  • Retain the objects by using their keys and providing the data, otherwise, you end up with an array of just numbers.
  • Since your aim is to raise salary by raise then instead of multiplying by the raise, in this case 10, multiply by 100+raise
  • You do not have to hard-code raise since you're passing it to the function; it allows you to pass 5, or 15 or some other raise value without changing your function.

const input = [{ name: "Ali", salary: 3000 }, { name: "Rob", salary: 2000 }, { name: "Adam", salary: 4500 }],
      increment = 10;
      
function raiseSalary(arr, raise) {
    if (arr.length === 0) {
        return [];
    } else {
        return arr.map((salaryObj) => {
            return ({name:salaryObj.name, salary:(salaryObj.salary / 100) * (100+raise)});
        });
    }
}

console.log( raiseSalary(input, increment) );

Alternatively ...

Just return the modified array without testing for elements, and you can also use object destructuring.

const input = [{ name: "Ali", salary: 3000 }, { name: "Rob", salary: 2000 }, { name: "Adam", salary: 4500 }],
      increment = 10;
      
function raiseSalary(arr, raise) {
    return arr.map(({name,salary}) => {
        return ({name, salary: salary/100*(100+raise)});
    });
}

console.log( raiseSalary(input, increment) );
console.log( raiseSalary([], increment) );

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!