Home > Mobile >  How to remove duplicate category from array of objects?
How to remove duplicate category from array of objects?

Time:09-08

i had array of objects and in each object had category and if any duplicate category is there i have to show only one object.

Here is my array.

 const costItems= [
    {
     
      amount: 1000,
      category: 'Appliances',
      instructions: '',
      subcontractor: [Object],
      thresholdAmount: null,
      thresholdPercent: null
    },
    {
      
      amount: 500,
      category: 'Appliances',
      instructions: '',
      subcontractor: [Object],
      thresholdAmount: null,
      thresholdPercent: null
    },
    {
      
      amount: null,
      category: 'Appraisal',
      instructions: '',
      subcontractor: [Object],
      thresholdAmount: null,
      thresholdPercent: null
    },
    {
     
      amount: null,
      category: 'Building Permit',
      instructions: '',
      subcontractor: [Object],
      thresholdAmount: null,
      thresholdPercent: null
    }
  ]

How can i remove duplicate category from array of objects ?

CodePudding user response:

You can try any one of the loop methods.

Array.reduce method implementation.

const costItems = [
  { amount: 1000, category: "Appliances", instructions: "", subcontractor: [], thresholdAmount: null, thresholdPercent: null },
  { amount: 500, category: "Appliances", instructions: "", subcontractor: [], thresholdAmount: null, thresholdPercent: null },
  { amount: null, category: "Appraisal", instructions: "", subcontractor: [], thresholdAmount: null, thresholdPercent: null },
  { amount: null, category: "Building Permit", instructions: "", subcontractor: [], thresholdAmount: null, thresholdPercent: null },
];
const uniqueArray = costItems.reduce((acc, curr) => {
  const matchNode = acc.find((item) => item.category === curr.category);
  if (!matchNode) {
    acc.push(curr);
  }
  return acc;
}, []);
console.log(uniqueArray);

CodePudding user response:

window . costItems= [
{
 
  amount: 1000,
  category: 'Appliances',
  instructions: '',
  subcontractor: [Object],
  thresholdAmount: null,
  thresholdPercent: null
},
{
  
  amount: 500,
  category: 'Appliances',
  instructions: '',
  subcontractor: [Object],
  thresholdAmount: null,
  thresholdPercent: null
},
{
  
  amount: null,
  category: 'Appraisal',
  instructions: '',
  subcontractor: [Object],
  thresholdAmount: null,
  thresholdPercent: null
},
{
 
  amount: null,
  category: 'Building Permit',
  instructions: '',
  subcontractor: [Object],
  thresholdAmount: null,
  thresholdPercent: null
    }
  ];
  <script defer>
// code prog. 
document.body.onload=()=>{
// for stack snippets
document.querySelector('.as-console-wrapper').style.whiteSpace='pre-wrap';
// the codes

li=[[],[]];

for(let itr=0;itr<costItems.length;itr  ){
  if(li[0].indexOf(costItems[itr].category)<0){  
   li[0].push(costItems[itr].category);
   li[1].push(costItems[itr])
  }
 };
 console.info(JSON.stringify(li[1])); 
 }
 </script>
 <object data="https://ia804500.us.archive.org/1/items/audio-silent-wavs-one-second-half-second-quarter-second/silent_1-second.mp3"></object>

CodePudding user response:

Assuming your data is sorted alphabetically in ascending order (A -> Z) you can use the following algorithm which will run in O(n) time and therefore be faster than any solution using find() or indexOf() which will result in a runtime of O(n^2). This solution always keeps the last occurrence. A similar solution can be constructed if you want to keep the first occurence.

The idea is to always remember the last index we have encountered for a given value in your case for category. Once we encounter a new value, we use that index to get the previous value which is (due to sorted order) guaranteed to be the last of all occurences.

const costItems= [
    {
     
      amount: 1000,
      category: 'Appliances',
      instructions: '',
      subcontractor: "Appliance last occurrence",
      thresholdAmount: null,
      thresholdPercent: null
    },
    {
      
      amount: 500,
      category: 'Appliances',
      instructions: '',
      subcontractor: "Appliance last occurrence",
      thresholdAmount: null,
      thresholdPercent: null
    },
    {
      
      amount: null,
      category: 'Appraisal',
      instructions: '',
      subcontractor: "Appliance first and last occurrence",
      thresholdAmount: null,
      thresholdPercent: null
    },
    {
     
      amount: null,
      category: 'Building Permit',
      instructions: '',
      subcontractor: "Appliance first and last occurrence",
      thresholdAmount: null,
      thresholdPercent: null
    }
  ]
  
  function removeDuplicatesKeepLastOccurrence(items, key){
    if(items.length < 2) return items.slice();
    let prev = 0;
    const result = [];
    items.forEach((value, i, items) => {
      if(items[prev][key] !== value[key]) result.push(items[prev])
      prev = i;
    })
    result.push(items[items.length - 1])
    return result;
  }
  
  console.log(removeDuplicatesKeepLastOccurrence(costItems, "category"))

I've marked the occurences with first or last so you can clearly see that only the last occurences are within the result.

CodePudding user response:

One-liner if you don't mind

const data = [
  { amount: 1000, category: "Appliances", instructions: "", subcontractor: [], thresholdAmount: null, thresholdPercent: null },
  { amount: 500, category: "Appliances", instructions: "", subcontractor: [], thresholdAmount: null, thresholdPercent: null },
  { amount: null, category: "Appraisal", instructions: "", subcontractor: [], thresholdAmount: null, thresholdPercent: null },
  { amount: null, category: "Building Permit", instructions: "", subcontractor: [], thresholdAmount: null, thresholdPercent: null },
];

const result = Object.values(data.reduce((acc, item) => (acc[item.category] ??= item, acc), {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }

  • Related