簡體   English   中英

JavaScript - 從 ES6 對象數組中計算並刪除重復項

[英]JavaScript - Count and remove duplicates from array of objects ES6

我基本上是在尋找一個簡潔的解決方案(可能使用 ES6)來減少重復對象的數組並計算重復的數量。

所以例如

const items = [
   {
     id: 1,
     type: 'PEPSI',
   },
  {
     id: 1,
     type: 'PEPSI',
   },
  {
     id: 1,
     type: '7UP',
   },
  {
     id: 1,
     type: 'FANTA',
   },
  {
     id: 2,
     type: 'FANTA',
   },
  {
     id: 2,
     type: '7UP',
   },
  {
     id: 2,
     type: '7UP',
   }
];

function getItems(data, id) {
      return data.filter(x => x.id === id).map((item) => {
        const obj = {};
        obj.quantity = **I want the quantity to go here**
        obj.type = item.type;
        return obj;
      });
    }

所以運行函數getItems(items, 1)會給我:

[
 { quantity: 2, type: 'PEPSI' },
 { quantity: 1, type: '7UP' },
 { quantity: 1, type: 'FANTA' }
]

提前致謝!

您可以使用此代碼:

 const items = [ { id: 1, type: 'PEPSI', }, { id: 1, type: 'PEPSI', }, { id: 1, type: '7UP', }, { id: 1, type: 'FANTA', }, { id: 2, type: 'FANTA', }, { id: 2, type: '7UP', }, { id: 2, type: '7UP', } ]; const getItems = (data) => { return data.reduce((a,b) => { const item = a.find(i => i.type === b.type); if(item){ item.quantity = item.quantity + 1; } else { a.push({ quantity: 0, type: b.type }); } return a; }, []); }; console.log(getItems(items)); const getItemsWithId = (data, id) => { return data.reduce((a,b) => { if(b.id === id) { const item = a.find(i => i.type === b.type); if(item){ item.quantity = item.quantity + 1; } else { a.push({ quantity: 0, type: b.type }); } } return a; }, []); }; console.log(getItemsWithId(items, 1));

另一個解決方案,希望這有幫助

 const items = [ { id: 1, type: 'PEPSI', }, { id: 1, type: 'PEPSI', }, { id: 1, type: '7UP', }, { id: 1, type: 'FANTA', }, { id: 2, type: 'FANTA', }, { id: 2, type: '7UP', }, { id: 2, type: '7UP', } ]; function getItems(items, id){ return Object.values(items.reduce((arr,curr) => { if(curr.id == id){ if(arr[curr.type]){ arr[curr.type] = { ...arr[curr.type], quantity: arr[curr.type].quantity + 1, } } else { arr[curr.type] = { quantity: 1, type: curr.type } } } else { arr[curr.type] = { quantity: 1, type: curr.type } } return arr; }, {})) } const arr = getItems(items, 1); console.log(arr);

我會做這樣的事情:

 function similar(needle, haystack, exact){ if(needle === haystack){ return true; } if(needle instanceof Date && haystack instanceof Date){ return needle.getTime() === haystack.getTime(); } if(!needle || !haystack || (typeof needle !== 'object' && typeof haystack !== 'object')){ return needle === haystack; } if(needle === null || needle === undefined || haystack === null || haystack === undefined || needle.prototype !== haystack.prototype){ return false; } var keys = Object.keys(needle); if(exact && keys.length !== Object.keys(haystack).length){ return false; } return keys.every(function(k){ return similar(needle[k], haystack[k]); }); } function Samer(array){ this.get = (id = null, type = null)=>{ const a = array.slice(), l = a.length, s = [], r = []; if(id !== null){ a.splice(0, l, ...a.filter(o=>o.id===+id)); } if(type !== null){ a.splice(0, l, ...a.filter(o=>o.type.match(new RegExp('^'+type+'$', 'i')))); } for(let o of a){ let m = false; for(let i=0,v,l=s.length; i<l; i++){ m = false; v = s[i]; if(similar(o, v, true)){ r[i].quantity++; m = true; break; } } if(!m){ s.push(o); r.push({...o, quantity:1}); } } return r; } } const items = [ { id: 1, type: 'PEPSI', }, { id: 1, type: 'PEPSI', }, { id: 1, type: '7UP', }, { id: 1, type: 'FANTA', }, { id: 2, type: 'FANTA', }, { id: 2, type: '7UP', }, { id: 2, type: '7UP', } ]; const samer = new Samer(items); console.log(samer.get()); console.log(samer.get(1)); console.log(samer.get(2)); console.log(samer.get(null, '7up')); console.log(samer.get(1, 'pepsi'));

請注意,這將適用於具有任何鍵的任何對象。

1.減少。

const items = [
          {
            id: 1,
            type: 'PEPSI',
          },
         {
            id: 1,
            type: 'PEPSI',
          },
         {
            id: 1,
            type: '7UP',
          },
         {
            id: 1,
            type: 'FANTA',
          },
         {
            id: 2,
            type: 'FANTA',
          },
         {
            id: 2,
            type: '7UP',
          },
         {
            id: 2,
            type: '7UP',
          }
       ];

const groupBy = (objectArray, property) => {
    return objectArray.reduce(function (total, obj) {
        let key = obj[property];
        if (!total[key]) {
            total[key] = 0;
        }
        total[key]+=1;
        return total;
    }, {});
 }

 let groupedArray = groupBy(items, 'type');

 console.log(groupedArray);

2.不減。

const items = [
      {
        id: 1,
        type: 'PEPSI',
      },
     {
        id: 1,
        type: 'PEPSI',
      },
     {
        id: 1,
        type: '7UP',
      },
     {
        id: 1,
        type: 'FANTA',
      },
     {
        id: 2,
        type: 'FANTA',
      },
     {
        id: 2,
        type: '7UP',
      },
     {
        id: 2,
        type: '7UP',
      }
   ];

const compressArray = original =>{
      var compressed = [];
      var copy = original.slice(0);
      for (var i = 0; i < original.length; i++) {

        var myCount = 0;    
        // loop over every element in the copy and see if it's the same
        for (var w = 0; w < copy.length; w++) {
          if (copy[w] && original[i].type == copy[w].type) {
            // increase amount of times duplicate is found
            myCount++;
            // sets item to undefined
            delete copy[w];
          }
        }

        if (myCount > 0) {
          var a = new Object();
          a.value = original[i].type;
          a.count = myCount;
          compressed.push(a);
        }
      }

      return compressed;
};

var counter = compressArray(items);
console.log(counter);

這是一個將返回你想要的,如果你想對許多數組做同樣的事情,你可以把它放在一個函數中,否則我認為它可以

 const items = [ { id: 1, type: 'PEPSI', }, { id: 1, type: 'PEPSI', }, { id: 1, type: '7UP', }, { id: 1, type: 'FANTA', }, { id: 2, type: 'FANTA', }, { id: 2, type: '7UP', }, { id: 2, type: '7UP', } ]; let tempObj = {}; for (const item of items) { if (tempObj[item.type] == undefined) { tempObj[item.type] = 1; }else{ tempObj[item.type] += 1; } } //console.log(tempObj) // And if you want to formet in a different way then let newTemp = []; for (const key in tempObj) { let objNew = { quantity : tempObj[key], type : key } newTemp.push(objNew); } console.log(newTemp)

您需要先減少然后列出項目

 const items = [ { id: 1, type: 'PEPSI', }, { id: 1, type: 'PEPSI', }, { id: 1, type: '7UP', }, { id: 1, type: 'FANTA', }, { id: 2, type: 'FANTA', }, { id: 2, type: '7UP', }, { id: 2, type: '7UP', } ]; let reducer = items.reduce(function (accumulator, currentElement) { let index = accumulator.findIndex(item=>item.id==currentElement.id && item.type==currentElement.type) if(index>=0){ accumulator[index].quantity+=1 return [...accumulator] }else{ return [...accumulator,{...currentElement,quantity:1}] } return accumulator }, []) let getItems = (items, id) => items.filter(item => item.id == id).map(item=>{return { type:item.type,quantity:item.quantity }}) console.log(getItems(reducer,1))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM