【问题标题】:array reduce accumulator as an empty array, push not working数组减少累加器为空数组,推送不起作用
【发布时间】:2020-05-28 22:05:50
【问题描述】:

为什么 accumulator.push 无效?累加器是一个空数组!?如果我使用预定义变量flarArray,一切正常!!我知道如果reduce回调中没有包含初始值,那么它将使用数组中的第一项(在我的情况下将是一个对象)作为累加器,但是如果我告诉它从一个数组开始作为累加器,这有什么问题?

const profiles = [
  {
    id: 1,
    userID: '1',
    favoriteMovieID: '1',
  },
  {
    id: 2,
    userID: '2',
    favoriteMovieID: '1',
  }
];

const users = {
  1: {
    id: 1,
    name: 'Jane Cruz',
    userName: 'coder',
  },
  2: {
    id: 2,
    name: 'Matthew Johnson',
    userName: 'mpage',
  }
};

const movies = {
  1: {
    id: 1,
    name: 'Planet Earth 1',
  },
  2: {
    id: 2,
    name: 'Selma',
  }
};
let flatArray = [];
const flattenedProfiles = profiles.reduce(function(accumulator, currentProfile) {  
  return accumulator.push({id: currentProfile.id, name: users[currentProfile.id].name, favoriteMovie: movies[currentProfile.favoriteMovieID].name})
},[]);

【问题讨论】:

  • push() 方法返回数组的新长度,所以在你的回调中你返回的是一个整数而不是累加器。你需要把return accumulator.push({ ... })改成accumulator.push({ ... }); return accumulator;

标签: javascript arrays


【解决方案1】:

您需要返回accumulator,而不是返回push 函数的返回值,即添加新项后数组的大小。在下一次迭代中,您将在整数上调用 push 函数

const flattenedProfiles = profiles.reduce((acc, curr) => {  
  acc.push({
     id: curr.id,
     name: users[curr.id].name,
     favoriteMovie: movies[curr.favoriteMovieID].name
  });

  return acc;
},[]);

【讨论】:

  • 我明白了,尝试在数组仍在填充时返回。我会记得制造阵列并完成return 最终阵列!
【解决方案2】:

Array.pushreduce 的回调返回中返回数组的新长度,您正在返回无法进行下一次推送操作的新长度。使用Array.concat 代替或在Array.push 之后也返回数组。

const flattenedProfilesPush = profiles.reduce((acc, curr) => {  
    acc.push({
        id: curr.id,
    })
    return acc
},[])


const flattenedProfilesConcat = profiles.reduce((acc, curr) =>  
    acc.concat({
        id: curr.id,
    }),[]
)

【讨论】:

    【解决方案3】:

    问题是你返回accumulator.push()。这将返回数组的长度,因此您的下一个累加器将是一个数字而不是数组。

    我最好返回一个数组并在累加器上使用扩展运算符。

    const profiles = [
      {
        id: 1,
        userID: '1',
        favoriteMovieID: '1',
      },
      {
        id: 2,
        userID: '2',
        favoriteMovieID: '1',
      }
    ];
    
    const users = {
      1: {
        id: 1,
        name: 'Jane Cruz',
        userName: 'coder',
      },
      2: {
        id: 2,
        name: 'Matthew Johnson',
        userName: 'mpage',
      }
    };
    
    const movies = {
      1: {
        id: 1,
        name: 'Planet Earth 1',
      },
      2: {
        id: 2,
        name: 'Selma',
      }
    };
    let flatArray = [];
    const flattenedProfiles = profiles.reduce(function(accumulator, currentProfile) {  
      return [...accumulator, {id: currentProfile.id, name: users[currentProfile.id].name, favoriteMovie: movies[currentProfile.favoriteMovieID].name}]
    },[]);
    
    console.log(flattenedProfiles);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2015-12-15
      • 2022-01-15
      • 2020-04-06
      • 2014-01-06
      • 1970-01-01
      相关资源
      最近更新 更多