简体   繁体   English

如何计算 javascript 中 arrays 的值的总和

[英]How to calculate the sum of the values of arrays in javascript

I'm new to programming and I'm having trouble to make an array treatment in javascript (vue).我是编程新手,在 javascript (vue) 中进行数组处理时遇到了麻烦。

Here's the deal: I have 26 arrays with 4 elements each (values: a, b, c, d).这是交易:我有 26 个 arrays,每个有 4 个元素(值:a、b、c、d)。

I have to sum the values according to their indexes, example:我必须根据它们的索引对值求和,例如:

{c, d, b, a} = c = 4, d = 3, b = 2, a = 1; {c, d, b, a} = c = 4, d = 3, b = 2, a = 1;

In the end I have to have an array with the sum of all these 26 objects, example: [a = 104, b = 78, c = 52, d = 26]最后我必须有一个包含所有这 26 个对象之和的数组,例如:[a = 104, b = 78, c = 52, d = 26]

What I currently have:我目前拥有的:

Array(26) [ (4) […], (4) […], (4) […], (4) […], (4) […], (4) […], (4) […], (4) […], (4) […], (4) […], … ]

this.array.forEach(item => {
    sum[item.value] = sum[item.value] + (i+1)
});

Edit: The objects of the array of 26 objects have value and description in different orders {b, a, d, c}, {a,d,b,c}, {a,b,c,d}... The indexes have "points" (value to sum), which is: the first index = 4 points, the second index = 3, the third = 2, the last = 1I have to sum the values of every single one and put in one array of the results.编辑:26 个对象数组的对象具有不同顺序的值和描述 {b, a, d, c}, {a,d,b,c}, {a,b,c,d}...索引有“点”(要求和的值),即:第一个索引 = 4 个点,第二个索引 = 3,第三个 = 2,最后一个 = 1我必须对每个单独的值求和并放入一个数组的结果。

So my expected result array is (for example):所以我的预期结果数组是(例如):

[a = 104, b = 78, c = 52, d = 26]

Edit2: Sorry guys, I didn't understood at first what you were asking for. Edit2:对不起,我一开始不明白你在问什么。 My simplied input array:我的简化输入数组:

Array(3) [
[
    {value: "a", description: "..."},
    {value: "b", description: "..."},
    {value: "c", description: "..."},
    {value: "d", description: "..."}
], 
[
    {value: "a", description: "..."},
    {value: "b", description: "..."},
    {value: "c", description: "..."},
    {value: "d", description: "..."}
], 
[
    {value: "a", description: "..."},
    {value: "b", description: "..."},
    {value: "c", description: "..."},
    {value: "d", description: "..."}
]]

I think it's more a logic problem, but I'm stuck at this.我认为这更像是一个逻辑问题,但我坚持这一点。

Thanks in advance!提前致谢!

To explain (possibly not very well) what this does:解释(可能不是很好)这是做什么的:

For every inner array it's taking each object and converting it into an array like ['a', 3] where 'a' is the value from the object and the 3 is 4 minus its index (so that the scores should be 4, 3, 2, 1 for the four positions in the inner array).对于每个内部数组,它获取每个 object 并将其转换为类似 ['a', 3] 的数组,其中 'a' 是 object 的值,3 是 4 减去它的索引(因此分数应该是 4, 3 , 2, 1 表示内部数组中的四个位置)。 The inner arrays are then flattened so that these new arrays are all at the same level in an array.然后将内部 arrays 展平,以便这些新的 arrays 在阵列中都处于同一级别。 These new arrays are the processed through reduce to add up the scores by the value that we've stored in the array.这些新的 arrays 是通过 reduce 处理的,以将分数与我们存储在数组中的值相加。

 const data = [ [{ value: 'a' }, { value: 'b' }, { value: 'c' }, { value: 'd' }], [{ value: 'b' }, { value: 'c' }, { value: 'd' }, { value: 'a' }] ]; const res = data.flatMap( (scores) => scores.map(({ value }, i) => [value, 4 - i]) ).reduce( (acc, [key, score]) => Object.assign(acc, { [key]: acc[key] + score }), { a: 0, b: 0, c: 0, d: 0 } ); console.log(res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM