我想计算一个对象内部出现相同值的次数,并创建一个添加了数量的新对象。
我尝试使用filter
,map
和reduce
,但是没有用。
我有此数据:
let arrayOfObjects = [
{name: 'Disney', type: 'inteira'},
{name: 'Bottieli', type: 'inteira'},
{name: 'Monster Truck', type: 'inteira'},
{name: 'Xuxa', type: 'desconto'},
{name: 'Pokémon', type: 'zaffari'},
]
我想要这样的输出(根据'type'键值并显示每个项目的数量,创建一个没有重复项目的新对象)
newArrayOfObjects = [
{name: 'Disney', type: 'inteira', quantity: 3},
{name: 'Xuxa', type: 'desconto', quantity: 1},
{name: 'Pokémon', type: 'zaffari', quantity: 1}
]
答案 0 :(得分:0)
有许多方法可以实现。一种方法是通过Array#reduce
方法构造一个映射,该映射将每个type
映射到具有以下内容的item
数据的相应count
(请注意,使用这样的映射是一种优化):
key
是项type
,而value
是项(带计数)type
键的值,请增加匹配项的计数type
键的值,请在reduce()
中插入要迭代的当前项的克隆,并为该项包括初始计数1
reduce()
创建的映射传递到Object.values()
,以提取平整的Array
项,并在缩减过程中计算出相应的计数。以下是一个有效的代码片段,可用于实际操作:
let arrayOfObjects = [
{name: 'Disney', type: 'inteira'},
{name: 'Bottieli', type: 'inteira'},
{name: 'Monster Truck', type: 'inteira'},
{name: 'Xuxa', type: 'desconto'},
{name: 'Pokémon', type: 'zaffari'},
]
/* Iterate arrayOfObjects and reduce() this to a temporary mapping where item counts
are aggregated. Once that mapping is built, we'll extract values of the mapping to
get the desired array result (ie with items, and type counts) */
let newArrayOfObjects = Object.values(arrayOfObjects.reduce((mapping, item) => {
/* Find exsiting item with matching item type in our mapping */
const { [item.type]:matchingItem } = mapping;
/* If matching item found, increment the count */
if(matchingItem) {
matchingItem.count ++;
}
/* Otherwise, insert item into mapping, and also include a starting count of one for it */
else {
mapping[ item.type ] = { ...item, count : 1 };
}
/* Return the updated mapping */
return mapping;
},{}))
console.log(newArrayOfObjects);
希望有帮助:-)