[英]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?当每个薪水都是对象的属性并且每个对象都存储在数组中时,如何更改所有薪水值并将它们增加一个百分比? Eg increasing by 10 percent and I have to round the result up to the nearest integer:
例如增加 10%,我必须将结果四舍五入到最接近的整数:
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. map 方法应该做你想做的事。 Note the destructuring of salaryObj to avoid manipulating the original object.
注意salaryObj的解构以避免操纵原始对象。
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+raise
raise
then 来提高薪水,而不是乘以raise
,在这种情况下10
,乘以100+raise
raise
since you're passing it to the function;raise
进行硬编码,因为您将它传递给函数; it allows you to pass 5
, or 15
or some other raise value without changing your function.5
或15
或其他一些提升值。 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) );
Here's a simple approach with map
and Object spreading:这是一种使用
map
和对象传播的简单方法:
const updateSalaries = (people, pct) => people .map (({salary, ...rest}) => ({ ...rest, salary: Math .round (salary * (100 + pct) / 100) })) const people = [{name: "Ali", salary: 3000}, {name: "Rob", salary: 2000}, {name: "Adam", salary: 4500}] console .log (updateSalaries (people, 10))
.as-console-wrapper {max-height: 100% !important; top: 0}
For each element, we destructure the input to extract the salary
from the rest of the object, and then create a new result containing the rest, but with a salary calculated from the original salary and the percentage adjustment.对于每个元素,我们解构输入以从对象的其余部分中提取
salary
,然后创建一个包含其余部分的新结果,但工资是根据原始工资和百分比调整计算得出的。
let userDetail=[
{ name: "Ali", salary: 3000 },
{ name: "Rob", salary: 2000 },
{ name: "Adam", salary: 4500 },
];
function getPercentage(num, percentage){
return num * (percentage / 100);
}
userDetail=userDetail.map(x=> {
return{
...x,
salary:x.salary+getPercentage(x.salary,10)
}
})
console.log(userDetail)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.