加载中... 用reduce替代map和filter - semyin's blog
174°

用reduce替代map和filter


const arr = [
  {
    id: 1,
    age: 20,
    name: '小华',
  },
  {
    id: 2,
    age: 21,
    name: '小红',
  },
  {
    id: 3,
    age: 22,
    name: '小蓝',
  },
  {
    id: 4,
    age: 23,
    name: '小黑',
  },
];

const arr2 = arr.reduce((pre, next) => {
  if (next.age <= 23) {
    const { id, name } = next;
    return pre.concat(
      Object.assign(
        {},
        {
          label: name,
          value: id,
        }
      )
    );
  } else {
    return pre;
  }
}, []);

console.log(arr2);

已有 0 条评论

    我有话说: