简体   繁体   English

Map 通过对象数组并添加具有相同属性值的元素的计数器

[英]Map through array of objects and add counter of elements with the same property value

Given following array:给定以下数组:

const arr = [
  {color: 'red'}, 
  {color: 'red'}, 
  {color: 'green'}, 
  {color: 'blue'}, 
  {color: 'blue'}, 
  {color: 'red'}]

how to map through its element to create an array of objects extended with property colorIndex which would work as a counter of how many previous elements have the same color property?如何通过其元素 map 创建一个对象数组,该数组使用属性colorIndex扩展,该数组将用作之前有多少元素具有相同color属性的计数器?

the result would be a following array:结果将是以下数组:

[
  {color: 'red', colorIndex: 0}, 
  {color: 'red', colorIndex: 1}, 
  {color: 'green', colorIndex: 0}, 
  {color: 'blue', colorIndex: 0}, 
  {color: 'blue', colorIndex: 1}, 
  {color: 'red', colorIndex: 2}
]

is there some elegant solution to achieve that?是否有一些优雅的解决方案来实现这一目标?

The first solution that comes to my mind isn't elegant but it will do the trick.我想到的第一个解决方案并不优雅,但它可以解决问题。 Best is to work with and extra object.最好使用额外的 object。

const foundItems = {};

arr.map(item => {
   let foundItem = foundItems[item.color] ? foundItems[item.color]+ 1 : 0;
   foundItems[item.color] = founditem;
   return {
      ...item,
      colorIndex: foundItem,
   }

})

Well, not sure why are you asking and what have you tried already to solve this.好吧,不知道你为什么要问,你已经尝试过什么来解决这个问题。 It's hard to get proper answer where there is no context at all.在根本没有上下文的情况下很难得到正确的答案。 But here is my approach, maybe you'll lern a bit of it:但这是我的方法,也许你会学到一点:

arr.map((elem, index) => {

  // we only care about values before current index, to see how many elements with given color were **before**
  var colors = arr.map((obj) => obj.color).splice(0, index + 1);

  return {
    ...elem, // using spread operator to get all properties of original object
    count: colors.reduce(
      (prev, curr) => (curr == elem.color ? prev + 1 : prev),
      -1 // starting from -1 to get 0-based counter
    ),
  };
})

See it on stackblitz here: https://stackblitz.com/edit/js-bnru4m在此处的 stackblitz 上查看: https://stackblitz.com/edit/js-bnru4m

Read more about reduce : https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce阅读有关减少的更多信息: https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Read more about spread operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax阅读有关扩展运算符的更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Read more about splicing an array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice阅读有关拼接数组的更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Read more about mapping an array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map阅读有关映射数组的更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM