如何从对象数组中获取唯一类别数据

时间:2018-10-21 10:49:40

标签: javascript arrays json object

我有一个这样的对象:

data.bills = [
    { date: '2018-01-20', amount: '220', category: 'Electricity' ,categoryId: '48' },
    { date: '2018-01-20', amount: '20', category: 'Gas' ,categoryId: '16' },
    { date: '2018-02-20', amount: '120', category: 'Electricity' ,categoryId: '48' }
]

现在,我想像这样创建一个新数组(要求输出):

data.allUniqueCategories = [
    {category: 'Electricity' ,categoryId: '48'},
    { category: 'Gas' ,categoryId: '16'}
]

我尝试过类似的方法,但是它变得太复杂了,谁能说出简单的解决方案,谢谢。.

function _toConsumableArray(arr) { 
    if (Array.isArray(arr)) { 
        for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
        arr2[i] = arr[i]; } 
        return arr2; 
        } else {
    return Array.from(arr); } 
}

c.data.categories = [].concat(_toConsumableArray(new Set(data.bills.map(function (bill) {
    return bill.state;
}))));

console.log(c.data.categories);
//['Electricity','Gas']

但是我想要这种格式

//[{category: 'Electricity' ,categoryId: '48'},{ category: 'Gas' ,categoryId: '16'}]

4 个答案:

答案 0 :(得分:1)

data.bills = [
    { date: '2018-01-20', amount: '220', category: 'Electricity' ,categoryId: '48' },
    { date: '2018-01-20', amount: '20', category: 'Gas' ,categoryId: '16' },
    { date: '2018-02-20', amount: '120', category: 'Electricity' ,categoryId: '48' }
]
data.allUniqueCategories = [...new Set(data.bills.map(o => o.category));

这里map正在获取类别并添加到新的Set中,Set只能包含唯一值,因此,如果存在重复的类别,则不会将其添加到Set中,完成map之后,它将由该运算符Set

...的值分布到新数组中

答案 1 :(得分:1)

您可以reducebills转换为以类别为键的对象,然后使用Object.values()转换为数组:

const bills = [{"date":"2018-01-20","amount":"220","category":"Electricity","categoryId":"48"},{"date":"2018-01-20","amount":"20","category":"Gas","categoryId":"16"},{"date":"2018-02-20","amount":"120","category":"Electricity","categoryId":"48"}]

const allUniqueCategories = Object.values(bills.reduce((r, { category, categoryId }) => {
  r[category] = { category, categoryId };

  return r;
}, {}));

console.log(allUniqueCategories);

答案 2 :(得分:1)

您可以将reduceMap方法一起用作累加器参数,以通过categorycategoryId道具创建唯一的数组。

const bills = [
  { date: '2018-01-20', amount: '220', category: 'Electricity' ,categoryId: '48' },
  { date: '2018-01-20', amount: '20', category: 'Gas' ,categoryId: '16' },
  { date: '2018-02-20', amount: '120', category: 'Electricity' ,categoryId: '48' }
]

const uniq = bills.reduce((r, {category, categoryId}) => {
  let key = `${category}|${categoryId}`;
  r.set(key, (r.get(key) || {category, categoryId}));
  return r;
}, new Map())

console.log([...uniq.values()])

答案 3 :(得分:1)

使用Array#reduce()创建一个以类别为键的对象,并获取该对象的值作为结果

const bills = [
    { date: '2018-01-20', amount: '220', category: 'Electricity' ,categoryId: '48' },
    { date: '2018-01-20', amount: '20', category: 'Gas' ,categoryId: '16' },
    { date: '2018-02-20', amount: '120', category: 'Electricity' ,categoryId: '48' }
]

const res = Object.values(bills.reduce((a,{category,categoryId})=>{
   a[category]={category, categoryId}
   return a;
},{}))

console.log(res)