Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda