admin管理员组文章数量:1401158
I have one question regarding the good usage of Array.reduce in Javascript. Supposing I have an array with some simple objects like this:
let values = [{value: 2}, {value: 3}, {value: 8}, {value: 11}];
And I want to add a "diff" property to each object to pute the difference between the current item value and the previous item value. I want something like this:
let values = [
{value: 2},
{value: 3, diff: 1},
{value: 8, diff: 5},
{value: 11, diff: 3}
];
To achieve this I decided to use the Array.reduce method. I´m using reduce because I can access the current item as well as the latest item but I´m not interested in the reduced array in this case.
values.reduce((acc, cur) => {
if (acc.value) {
cur.diff = cur.value - acc.value;
}
return cur;
}, {});
Is this a good usage of Array.reduce? I don´t like the way I´m mutating the items of the original array but on the other way reduce is the only array method that can access the previous item. Maybe a simple for-of loop should be better in this case?
Thanks
I have one question regarding the good usage of Array.reduce in Javascript. Supposing I have an array with some simple objects like this:
let values = [{value: 2}, {value: 3}, {value: 8}, {value: 11}];
And I want to add a "diff" property to each object to pute the difference between the current item value and the previous item value. I want something like this:
let values = [
{value: 2},
{value: 3, diff: 1},
{value: 8, diff: 5},
{value: 11, diff: 3}
];
To achieve this I decided to use the Array.reduce method. I´m using reduce because I can access the current item as well as the latest item but I´m not interested in the reduced array in this case.
values.reduce((acc, cur) => {
if (acc.value) {
cur.diff = cur.value - acc.value;
}
return cur;
}, {});
Is this a good usage of Array.reduce? I don´t like the way I´m mutating the items of the original array but on the other way reduce is the only array method that can access the previous item. Maybe a simple for-of loop should be better in this case?
Thanks
Share Improve this question asked Mar 6, 2017 at 14:10 Federico GarcíaFederico García 1412 silver badges9 bronze badges 2- From the doc : The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value. – Weedoze Commented Mar 6, 2017 at 14:15
- 1 It is personal preference if it is good usage. You are using reduce to hold a value instead of its intended purpose. Some people will find it smart, others will hate it. – epascarello Commented Mar 6, 2017 at 14:16
2 Answers
Reset to default 7You could use Array#forEach
, because you have already the access to the predecessor.
The advantage is, you do not have the overhead of having a return value.
let values = [{ value: 2 }, { value: 3 }, { value: 8 }, { value: 11 }];
values.forEach((a, i, aa) => i && (a.diff = a.value - aa[i - 1].value));
console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can also use map()
in case you don't want to mutate the original array / items.
const values = [{ value: 2 }, { value: 3 }, { value: 8 }, { value: 11 }];
const diff = values.map(
({ value }, i, arr) => ({
value,
// add `diff` property if not first element
...(i && { diff: value - arr[i - 1].value }),
}),
);
console.log(diff);
本文标签: javascriptUse reduce to mutate array items Bad patternStack Overflow
版权声明:本文标题:javascript - Use reduce to mutate array items. Bad pattern? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744268922a2598084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论