从对象数组中删除第二次出现的对象[重复]

Posted

技术标签:

【中文标题】从对象数组中删除第二次出现的对象[重复]【英文标题】:Remove the second occurance of an object from an object array [duplicate] 【发布时间】:2022-01-20 12:11:38 【问题描述】:

我有一个我称之为fareZone 对象的数组。每个票价区对象都有一个 stops 数组。

我希望保留stop 对象的第一次出现,并从数组中删除同一对象的所有其他出现(同样意味着它具有相同的atcoCode)。

对象数组

const fareZones = [
    name: 'Zone 1',
    stops: [
            stopName: 'Ashton Bus Station',
            atcoCode: '1800EHQ0081',
        ,
        
            stopName: 'New Street',
            atcoCode: '1800EHQ0721',
        ,
        
            stopName: 'Farfield Road',
            atcoCode: '1800EHQ0722',
        ,
        
            stopName: 'Ashton Bus Station',
            atcoCode: '1800EHQ0081',
        ,
        
            stopName: 'New Street',
            atcoCode: '1800EHQ0041',
        ,
    ],
    prices: [],
,

    name: 'Zone 2',
    stops: [
        stopName: 'Henrietta Street',
        atcoCode: '1800EH24201',
     
    , ],
    prices: [],
,

    name: 'Zone 3',
    stops: [
            stopName: 'Crickets Ln',
            atcoCode: '1800EH24151',
        ,
        
            stopName: 'Tameside College',
            atcoCode: '1800EH21241',            
        ,
        
            stopName: 'Ashton Bus Station',
            atcoCode: '1800EHQ0081',            
        ,
    ],
    prices: [],
]

期望的结果

const fareZones = [
    name: 'Zone 1',
    stops: [
            stopName: 'Ashton Bus Station',
            atcoCode: '1800EHQ0081',
        ,
        
            stopName: 'New Street',
            atcoCode: '1800EHQ0721',
        ,
        
            stopName: 'Farfield Road',
            atcoCode: '1800EHQ0722',
        ,
        
            stopName: 'New Street',
            atcoCode: '1800EHQ0041',
        ,
    ],
    prices: [],
,

    name: 'Zone 2',
    stops: [
        stopName: 'Henrietta Street',
        atcoCode: '1800EH24201',
     
    , ],
    prices: [],
,

    name: 'Zone 3',
    stops: [
            stopName: 'Crickets Ln',
            atcoCode: '1800EH24151',
        ,
        
            stopName: 'Tameside College',
            atcoCode: '1800EH21241',            
        ,
        
            stopName: 'Ashton Bus Station',
            atcoCode: '1800EHQ0081',            
        ,
    ],
    prices: [],
]

注意 Zone 1 中的第三站是如何被删除的,因为它是重复的。

如何在 javascript 中实现这一点?

当前进度

// for each fare stage, check to see if we have duplicate stops
fareZones.map((fz) => 
    let stopsWithinFareZone = fz.stops;

    const atcoCodesOfStopsWithinFareZone = stopsWithinFareZone.map((s) => s.atcoCode);

    const hasDuplicateStops = hasDuplicates(atcoCodesOfStopsWithinFareZone);

    if (hasDuplicateStops) 
        // so we have duplicates, how do I keep the first occurrence
        // and remove all other occurrences of the duplicate stop
    
);
export const hasDuplicates = (array: string[]): boolean => 
    return new Set(array).size !== array.length;
;

【问题讨论】:

【参考方案1】:

您可以使用Setfilter 轻松实现结果:

const result = fareZones.map((fareZone) => 
      const set = new Set();
      const stops = fareZone.stops.filter((o) => 
        if (!set.has(o.atcoCode)) 
          set.add(o.atcoCode);
          return true;
         else return false;
      );
      return  ...fareZone, stops ;
    );

const fareZones = [
  
    name: "Zone 1",
    stops: [
      
        stopName: "Ashton Bus Station",
        atcoCode: "1800EHQ0081",
      ,
      
        stopName: "New Street",
        atcoCode: "1800EHQ0721",
      ,
      
        stopName: "Farfield Road",
        atcoCode: "1800EHQ0722",
      ,
      
        stopName: "Ashton Bus Station",
        atcoCode: "1800EHQ0081",
      ,
      
        stopName: "New Street",
        atcoCode: "1800EHQ0041",
      ,
    ],
    prices: [],
  ,
  
    name: "Zone 2",
    stops: [
      
        stopName: "Henrietta Street",
        atcoCode: "1800EH24201",
      ,
    ],
    prices: [],
  ,
  
    name: "Zone 3",
    stops: [
      
        stopName: "Crickets Ln",
        atcoCode: "1800EH24151",
      ,
      
        stopName: "Tameside College",
        atcoCode: "1800EH21241",
      ,
      
        stopName: "Ashton Bus Station",
        atcoCode: "1800EHQ0081",
      ,
    ],
    prices: [],
  ,
];

const result = fareZones.map((fareZone) => 
  const set = new Set();
  const stops = fareZone.stops.filter((o) => 
    if (!set.has(o.atcoCode)) 
      set.add(o.atcoCode);
      return true;
     else return false;
  );
  return  ...fareZone, stops ;
);

console.log(result);

【讨论】:

以上是关于从对象数组中删除第二次出现的对象[重复]的主要内容,如果未能解决你的问题,请参考以下文章

根据具有不同对象的匹配字段从数组列表中删除重复元素

从对象数组中的嵌套数组中删除重复项

jQuery / Javascript从对象数组中删除一个对象[重复]

Coffeescript / Node.js:如何从对象数组中删除重复对象?

js 数组遍历时删除元素

如何从对象数组中删除所有重复项?