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;
});
}
}
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);
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.
You're almost there. Just a few points
raise then instead of multiplying by the raise, in this case 10, multiply by 100+raiseraise 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) );