无法使用Reduce读取未定义的属性

时间:2017-05-02 05:15:51

标签: javascript higher-order-functions

我有一个带销售数字的对象,想要获得最高销售商的名称和销售额,但我一直收到错误,无法读取未定义的属性:

var salesTeam = [{name: {first: 'aleen', last: 'atkins'}, age: 26, sales: '$2314'},
        {name: {first: 'alvaro', last: 'angelos'}, age: 55, sales: '$1668'},
        {name: {first: 'denese', last: 'dossett'}, age: 29, sales: '$9248'},
        {name: {first: 'douglas', last: 'denney'}, age: 53, sales: '$5058'},
        {name: {first: 'earline', last: 'erickson'}, age: 19, sales: '$18876'},
        {name: {first: 'herman', last: 'hazell'}, age: 25, sales: '$2746'},
        {name: {first: 'homer', last: 'hirth'}, age: 26, sales: '$474'},
        {name: {first: 'hwa', last: 'heidt'}, age: 53, sales: '$9607'},
        {name: {first: 'hyon', last: 'hampshire'}, age: 46, sales: '$13598'},
        {name: {first: 'issac', last: 'ingerson'}, age: 45, sales: '$5225'},
        {name: {first: 'jeraldine', last: 'joplin'}, age: 39, sales: '$2891'},
        {name: {first: 'jin', last: 'jeffrey'}, age: 17, sales: '$14402'},
        {name: {first: 'joleen', last: 'jolin'}, age: 45, sales: '$15736'},
        {name: {first: 'jude', last: 'jarrett'}, age: 53, sales: '$7557'},
        {name: {first: 'magda', last: 'mireles'}, age: 18, sales: '$1498'},
        {name: {first: 'mistie', last: 'montealegre'}, age: 31, sales: '$6920'},
        {name: {first: 'nancy', last: 'napoli'}, age: 49, sales: '$5255'},
        {name: {first: 'regine', last: 'rohrbaugh'}, age: 33, sales: '$7881'},
        {name: {first: 'rolando', last: 'riebel'}, age: 35, sales: '$8573'},
        {name: {first: 'scarlett', last: 'stagg'}, age: 36, sales: '$7126'},
        {name: {first: 'sherron', last: 'strawn'}, age: 36, sales: '$8848'},
        {name: {first: 'susan', last: 'shilling'}, age: 29, sales: '$8542'},
        {name: {first: 'tama', last: 'tworek'}, age: 16, sales: '$9200'},
        {name: {first: 'tonisha', last: 'taunton'}, age: 41, sales: '$5219'},
        {name: {first: 'vergie', last: 'villescas'}, age: 25, sales: '$8712'}];

var highestEarner = function (companies) {
  var ret = companies.reduce(function(acc,element,i){

    if (parseInt(element['sales'].slice(1)) > acc['sales']){
      acc['sales'] = element['sales'];
      acc['name'] = element['name']['first'] +" "+ element['name']['last'];
    }
  },{sales: 0, name: ''});

  return ret;
};

console.log(highestEarner(salesTeam));

2 个答案:

答案 0 :(得分:2)

您需要在每次迭代中返回累加器,即在return acc之后缺少if {...}。此外,您还要将货币格式的值分配给acc['sales'],因此您也需要sliceparseInt(或只是存储数值)。



const salesTeam = [{"name":{"first":"aleen","last":"atkins"},"age":26,"sales":"$2314"},{"name":{"first":"alvaro","last":"angelos"},"age":55,"sales":"$1668"},{"name":{"first":"denese","last":"dossett"},"age":29,"sales":"$9248"},{"name":{"first":"douglas","last":"denney"},"age":53,"sales":"$5058"},{"name":{"first":"earline","last":"erickson"},"age":19,"sales":"$18876"},{"name":{"first":"herman","last":"hazell"},"age":25,"sales":"$2746"},{"name":{"first":"homer","last":"hirth"},"age":26,"sales":"$474"},{"name":{"first":"hwa","last":"heidt"},"age":53,"sales":"$9607"},{"name":{"first":"hyon","last":"hampshire"},"age":46,"sales":"$13598"},{"name":{"first":"issac","last":"ingerson"},"age":45,"sales":"$5225"},{"name":{"first":"jeraldine","last":"joplin"},"age":39,"sales":"$2891"},{"name":{"first":"jin","last":"jeffrey"},"age":17,"sales":"$14402"},{"name":{"first":"joleen","last":"jolin"},"age":45,"sales":"$15736"},{"name":{"first":"jude","last":"jarrett"},"age":53,"sales":"$7557"},{"name":{"first":"magda","last":"mireles"},"age":18,"sales":"$1498"},{"name":{"first":"mistie","last":"montealegre"},"age":31,"sales":"$6920"},{"name":{"first":"nancy","last":"napoli"},"age":49,"sales":"$5255"},{"name":{"first":"regine","last":"rohrbaugh"},"age":33,"sales":"$7881"},{"name":{"first":"rolando","last":"riebel"},"age":35,"sales":"$8573"},{"name":{"first":"scarlett","last":"stagg"},"age":36,"sales":"$7126"},{"name":{"first":"sherron","last":"strawn"},"age":36,"sales":"$8848"},{"name":{"first":"susan","last":"shilling"},"age":29,"sales":"$8542"},{"name":{"first":"tama","last":"tworek"},"age":16,"sales":"$9200"},{"name":{"first":"tonisha","last":"taunton"},"age":41,"sales":"$5219"},{"name":{"first":"vergie","last":"villescas"},"age":25,"sales":"$8712"}];

var highestEarner = function (companies) {
  var ret = companies.reduce(function(acc,element) {
    let currentSales = parseInt(element.sales.slice(1))
    if (currentSales > acc.sales) {
      acc.sales = currentSales;
      acc.name = `${element.name.first} ${element.name.last}`
    }
    return acc;
  },{sales: 0, name: ''});

  return ret;
};

console.log(highestEarner(salesTeam));




答案 1 :(得分:0)

使用reducer时的简单规则是,无论数组的每个元素中的数据格式如何,请确保每次运行都返回相同的格式,包括所有元素,以便它可以将返回值用作累加器(acc) 用于下一次运行。

我从下面学到的一个简单的错误:

enter image description here

enter image description here

这解决了问题。