簡體   English   中英

使用 angular 4 刪除對象數組中的重復項

[英]Removing duplicates with in an object array using angular 4

我有一個包含以下項目列表的數組,如圖所示,我想刪除重復項
[L7-LO, %L7-LO]來自該陣列。

有重復的數組
我嘗試過以下條件:

場景一:

this.formulalist.filter((el, i, a) => i == a.indexOf(el))

場景2:

Observable.merge(this.formulalist).distinct((x) => x.Value)
          .subscribe(y => {
       this.formulalist.push(y)
   });

場景3:

this.formulalist.forEach((item, index) => {
        if (index !== this.formulalist.findIndex(i => i.Value == item.Value)) 
        {
            this.formulalist.splice(index, 1);
        }

    });

上述三種情況都無法從該數組中刪除重復項。 有人可以幫忙解決這個查詢嗎?

angular 不是必需的,使用 vanillajs 過濾僅出現一次的元素並將第一次出現的元素添加到新列表中

let newFormulalist =  formulalist.filter((v,i) => formulalist.findIndex(item => item.value == v.value) === i);

嘗試填充一個沒有重復的新數組。 稍后將新數組分配給公式列表。

newArr = []
this.formulalist.forEach((item, index) => {
    if (this.newArr.findIndex(i => i.Value == item.Value) === -1) 
    {
        this.newArr.push(item)
    }

});
this.formulalist = this.newArr

編輯

看看上面的答案,解決方案似乎已經過時了。 更好的方法是使用Array.filter()不是Array.forEach()

但是,有一個更好的解決方案會很好,現在當我看到這個問題時,我覺得findIndex()由於額外的遍歷而不是一個好的方法。

我可能有一個Set並將值存儲在我想要過濾的Set ,如果Set有這些條目,我會從數組中跳過這些元素。

或者更好的方法是 Akitha_MJ 使用的方法,非常簡潔。 數組長度的一個循環,循環中的一個 Object(Map),鍵是我們要刪除重復項的值,而值是完整的 Object(Array 元素) 本身。 在循環中重復元素時,元素將在 Map 中被簡單地替換。 稍后只需從 Map 中取出值。

const result = Array.from(this.item.reduce((m, t) => m.set(t.name, t), new Map()).values());

希望這有效!!

// user reduce method to remove duplicates from object array , very easily

  this.formulalist= this.formulalist.reduce((a, b) => {
      if (!a.find(data => data.name === b.name)) {
        a.push(b);
      }
      return a;
    }, []);

// o/p = in formulalist you will find only unique values

使用 reducer 返回一個新的唯一對象數組:

 const input = [{ value: 'L7-LO', name: 'L7-LO' }, { value: '%L7-LO', name: '%L7-LO' }, { value: 'L7-LO', name: 'L7-LO' }, { value: '%L7-LO', name: '%L7-LO' }, { value: 'L7-L3', name: 'L7-L3' }, { value: '%L7-L3', name: '%L7-L3' }, { value: 'LO-L3', name: 'LO-L3' }, { value: '%LO-L3', name: '%LO-L3' } ]; console.log(input.reduce((acc, val) => { if (!acc.find(el => el.value === val.value)) { acc.push(val); } return acc; }, []));

通過將值分配給某些對象屬性,過濾唯一值要快得多 - 不會有重復項。 這種方法隨着初始數組的每個 +1 成員變得越來越好,因為循環將導致快速算法復雜化

 let arr = [ {value: 'L7-LO', name: 'L7-LO'}, {value: '%L7-LO', name: '%L7-LO'}, {value: 'L7-LO', name: 'L7-LO'}, {value: '%L7-LO', name: '%L7-LO'}, {value: 'L7-L3', name: 'L7-L3'}, {value: '%L7-L3', name: '%L7-L3'}, {value: 'LO-L3', name: 'LO-L3'}, {value: '%LO-L3', name: '%LO-L3'} ]; let obj = {}; const unique = () => { let result = []; arr.forEach((item, i) => { obj[item['value']] = i; }); for (let key in obj) { let index = obj[key]; result.push(arr[index]) } return result; } arr = unique(); // for example; console.log(arr);

如果您使用 ES6 及更高版本,使用 map 和 filter 函數的基本 JS 會很容易。

 var array = [{value:"a"},{value:"b"},{value:"c"},{value:"a"},{value:"c"},{value:"d"}]; console.log(array.filter((obj, pos, arr) => { return arr.map(mapObj => mapObj["value"]).indexOf(obj["value"]) === pos; }));

暫無
暫無

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

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