从对象数组中减少属性值的最佳方法

use*_*206 1 javascript

我试图找到最简洁的代码行,它遍历一个简单的对象数组,并将所有对象具有的特定属性的总值相加。

以这个对象数组为例:

const reviews = [
    {
      "name": "Joe Bloggs",
      "comment": "I liked it. Ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
      "rating": 4,
      "date": "22 April 2020",
      "init": true,
      "current": null
    },
    {
      "name": "Nick Smyth",
      "comment": "Great, thanks.",
      "rating": 4.5,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Mary Manson",
      "comment": "Amazing - consectetur adipiscing elit ut aliquam purus sit amet luctus.",
      "rating": 5,
      "date": "07 March 2020",
      "init": false,
      "current": null
    },
    {
      "name": "Rachel Stevens",
      "comment": "Pretty good. Quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
      "rating": 4.5,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Jack Hawthorn",
      "comment": "Aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
      "rating": 4,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Jacob Haywood",
      "comment": "Awful. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
      "rating": 1,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Ben Stiller",
      "comment": "Thanks a lot.",
      "rating": 4.5,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Xiao Wei",
      "comment": "Great, quis nostrud exercitation ullamco laboris nisi.",
      "rating": 4.5,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "John Harwood",
      "comment": "Rubbish - quis nostrud exercitation ullamco laboris nisi.",
      "rating": 2,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Jackie Chan",
      "comment": "Not great - quis nostrud exercitation ullamco laboris nisi.",
      "rating": 2.5,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Chuck Norris",
      "comment": "A poor 1.5 - quis nostrud exercitation ullamco laboris nisi.",
      "rating": 1.5,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Duncan Jones",
      "comment": "Satisfactory. Quis nostrud exercitation ullamco laboris nisi.",
      "rating": 3,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Peter Lahm",
      "comment": "Was ok. Quis nostrud exercitation ullamco laboris nisi.",
      "rating": 3.5,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Simon Arnold",
      "comment": "Nice - quis nostrud exercitation ullamco laboris nisi.",
      "rating": 4,
      "date": "29 December 2019",
      "init": false,
      "current": null
    },
    {
      "name": "Claire Pullen",
      "comment": "Great - quis nostrud exercitation ullamco laboris nisi.",
      "rating": 4.5,
      "date": "29 December 2019",
      "init": false,
      "current": null
    }
  ]
Run Code Online (Sandbox Code Playgroud)

每个对象都有一个“评级”属性,我想要一个所有这些属性总数的变量 - 所以我写了这个,它有效:

var correct = reviews.map(review => review.rating).reduce((a, b) => a + b); // correctly returns 53
Run Code Online (Sandbox Code Playgroud)

但是上面的感觉太冗长了,所以我尝试只对每个对象属性使用reduce方法,而不是映射来获取数组中的每个值,然后将该数组减少到一个值:

var incorrect = reviews.reduce((total, cur) => total + cur.rating); // incorrectly returns "[object Object]4.554.5414.54.522.51.533.544.5"
Run Code Online (Sandbox Code Playgroud)

为什么上面的不也返回53?有没有办法解决这个问题,使其只能使用reduce方法返回53?

感谢您的建议。

mat*_*eco 5

您必须将计数器变量total初始化为 0:

var incorrect = reviews.reduce((total, cur) => total + cur.rating, 0);
Run Code Online (Sandbox Code Playgroud)

否则,在第一次迭代中,total 变量将保存数组第一个元素的值而不是 0。

请参阅Array.prototype.reduce 文档

initialValue:用作第一次回调调用的第一个参数的值。如果未提供 initialValue,则数组中的第一个元素将用作初始累加器值并作为 currentValue 跳过。