Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda