JavaScript:从数组中删除所有在单个属性中相等的对象

JavaScript: Removing all Objects that are equal in just a single attribute from Array

我有一个这样的地理位置对象数组:

   let markers = [
   {
    "markerOffset": 10,
    "name": "Plantation",
    "coordinates": ["9.85804","53.5233"]
   },
   {
    "markerOffset": 10,
    "name": "Roasting",
    "coordinates": ["29.85804","50.5233"]
   },
   {
    "markerOffset": 10,
    "name": "Packaging",
    "coordinates": ["29.85804","50.5233"]
   },
{
    "markerOffset": 10,
    "name": "Harbour",
    "coordinates": ["21.85804","51.5213"]
   },
]

如何过滤数组以删除具有相同坐标的重复对象(例如元素 2 和 3)并且只留下一个具有坐标的条目。

由于 name: 属性不同

,Set 无法正常工作

简单比较并构建一个新数组。这不关心名称属性,只使用出现的第一个

let markers = [
   { "markerOffset": 10, "name": "Plantation", "coordinates": ["9.85804","53.5233"] },
   { "markerOffset": 10, "name": "Roasting", "coordinates": ["29.85804","50.5233"] },
   { "markerOffset": 10, "name": "Packaging", "coordinates": ["29.85804","50.5233"] },
   { "markerOffset": 10, "name": "Harbour",  "coordinates": ["21.85804","51.5213"] },
]

let results = [];
markers.forEach(marker => {
    if(!results.find(m => m.coordinates[0] === marker.coordinates[0] &&  m.coordinates[1] === marker.coordinates[1] )) results.push(marker);
})

console.log(results);

您需要遍历 markers 数组,将唯一标记(按其坐标)添加到新数组。第二个数组将用于比较循环中的下一个标记。

const markers = [
  {
    markerOffset: 10,
    name: "Plantation",
    coordinates: ["9.85804", "53.5233"],
  },
  {
    markerOffset: 10,
    name: "Roasting",
    coordinates: ["29.85804", "50.5233"],
  },
  {
    markerOffset: 10,
    name: "Packaging",
    coordinates: ["29.85804", "50.5233"],
  },
  {
    markerOffset: 10,
    name: "Harbour",
    coordinates: ["21.85804", "51.5213"],
  },
];

const filteredMarkers = [];

for (let i = 0, length = markers.length; i < length; i++) {
  let duplicate = false;
  for (let j = 0, length = filteredMarkers.length; j < length; j++) {
    const [lat1, lon1] = markers[i].coordinates;
    const [lat2, lon2] = filteredMarkers[j].coordinates;
    if (lat1 === lat2 && lon1 === lon2) {
      duplicate = true;
      break;
    }
  }
  if (!duplicate) {
    filteredMarkers.push(markers[i]);
  }
}

console.log(filteredMarkers);

一个很好的简单方法是使用 findIndex 使用坐标,你可以 toString 这些使查找更容易,在过滤器中使用它,如果索引相同这是重复或不是重复中的第一个。

例如

const markers = [
  {
    markerOffset: 10,
    name: "Plantation",
    coordinates: ["9.85804", "53.5233"],
  },
  {
    markerOffset: 10,
    name: "Roasting",
    coordinates: ["29.85804", "50.5233"],
  },
  {
    markerOffset: 10,
    name: "Packaging",
    coordinates: ["29.85804", "50.5233"],
  },
  {
    markerOffset: 10,
    name: "Harbour",
    coordinates: ["21.85804", "51.5213"],
  },
];

console.log(
  markers.filter(
    (m,ix) => markers.findIndex(c =>
      c.coordinates.toString() ===
      m.coordinates.toString()) === ix)
);

奖金,如果你想要重复的最后一个,你可以将 findIndex 更改为 findLastIndex

另一种使数组唯一的方法 属性:

const markers=[{markerOffset:10,name:"Plantation",coordinates:["9.85804","53.5233"]},{markerOffset:10,name:"Roasting",coordinates:["29.85804","50.5233"]},{markerOffset:10,name:"Packaging",coordinates:["29.85804","50.5233"]},{markerOffset:10,name:"Harbour",coordinates:["21.85804","51.5213"]}];

const uniqByCoords = Object.values(markers.reduce((acc, market) => {
    acc[String(market.coordinates)] ??= market; //return first market item in array as uniq. 
    // If you need the last one replace ??= to =
    return acc;
}, {}));

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