0

I have an object like this:

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' }
]

Now i want to create a new array from this like this(Req Output):

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

I tried something like this but its becoming too complex can anyone tell the simple solution thanks..

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']

but i want in this format

//[{category: 'Electricity' ,categoryId: '48'},{ category: 'Gas' ,categoryId: '16'}]
Mr world wide
  • 4,696
  • 7
  • 43
  • 97
  • Possible duplicate of [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Jeff Oct 21 '18 at 10:57

4 Answers4

2
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));

Here map is getting categories and adding to the new Set, Set can contain only unique values, so if there will be duplicate categories, it will not be added in Set, after finishing map it will spread Set values into new array by this operator ...

Artyom Amiryan
  • 2,846
  • 1
  • 10
  • 22
1

You can reduce the bills into an object with the category as the key, and than convert to array using 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);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1

You could use reduce method with Map as accumulator param to create unique array by category and categoryId props.

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()])
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
1

Use Array#reduce() to create an object with the categories as keys and get the values of that object as result

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)
charlietfl
  • 170,828
  • 13
  • 121
  • 150