最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Implement map using reduce - Stack Overflow

programmeradmin4浏览0评论
const items = [{
  id: 1,
  name: 'ball'
},{
  id: 2,
  name: 'axe'
}]

//says I want to update item 2
let input = 'laptop', id = 2

updated_item = items.map(o => o.id === id ? ({ ...o, name: input }) : o) });

console.log(updated_item) // worked

but just curious I also know that reduce is the base for map, foreEach and filter, I would want to try to implement above logic using reduce

const updated_item_using_reduce = items.reduce((accum, o, i) => {
  if(o.id === id) {
    //what to do here? change value of name put o into accum? by using push?
  }
  //else have to assign o to accum, but can't use push I think
}, [])
const items = [{
  id: 1,
  name: 'ball'
},{
  id: 2,
  name: 'axe'
}]

//says I want to update item 2
let input = 'laptop', id = 2

updated_item = items.map(o => o.id === id ? ({ ...o, name: input }) : o) });

console.log(updated_item) // worked

but just curious I also know that reduce is the base for map, foreEach and filter, I would want to try to implement above logic using reduce

const updated_item_using_reduce = items.reduce((accum, o, i) => {
  if(o.id === id) {
    //what to do here? change value of name put o into accum? by using push?
  }
  //else have to assign o to accum, but can't use push I think
}, [])
Share Improve this question asked May 7, 2018 at 5:45 user9728810user9728810 3
  • 1 So why exactly can't you use push? – JJJ Commented May 7, 2018 at 5:48
  • I don't think reduce is a base for forEach as there is no value returned. It's plain old iteration – Phil Commented May 7, 2018 at 5:49
  • @xianshenglu forEach iterates one-by-one as well. – CertainPerformance Commented May 7, 2018 at 5:53
Add a ment  | 

4 Answers 4

Reset to default 11

Similar to other's answers just more succinct:

items.reduce((accum, o, i) => {
  return [...accum, (o.id === id) ? {...o, name: input} : o];
}, []);

This implementation of Array.map() using Array.reduce() demonstrates how it works. As you can see the reduce only responsibility is to push (or concat) items that the callback returns:

const items = [{
  id: 1,
  name: 'ball'
},{
  id: 2,
  name: 'axe'
}]

//says I want to update item 2
let input = 'laptop', id = 2

const map = (arr, cb) => arr.reduce((r, o, i, a) => {
  r.push(cb(o, i, a));
  
  return r;
}, []);

const updated_item = map(items, (o) => o.id === id ? ({ ...o, name: input }) : o);

console.log(updated_item) // worked

You can use push, but like with most reduce functions, you have to make sure to return the accumulator:

const items = [{
  id: 1,
  name: 'ball'
}, {
  id: 2,
  name: 'axe'
}]

//says I want to update item 2
const input = 'laptop';
const id = 2;

const updated_items = items.reduce((accum, item) => {
  accum.push(item.id !== id
    ? item
    : ({ ...item, name: input })
  );
  return accum;
}, []);

console.log(updated_items)

You can try the following:

const items = [{
  id: 1,
  name: 'ball'
},{
  id: 2,
  name: 'axe'
}]

let input = 'laptop', id = 2
const updated_item_using_reduce = items.reduce((accum, o, i) => {
  if(o.id === id) {
    o.name = input;
    accum[i] = o;
  }
  else{
    accum[i] = o;
  }
  return accum
  
}, []);

console.log(updated_item_using_reduce)

发布评论

评论列表(0)

  1. 暂无评论