从对象数组中删除第二次出现的对象
Remove the second occurance of an object from an object array
我有一个我称之为 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: [],
}]
请注意 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;
};
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);
我有一个我称之为 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: [],
}]
请注意 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;
};
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);