如何仅在对象不存在时才向数组注入对象?



我有我的空数组

locations = []

我还有另一个带有位置和城市的数组

Data = [
{ location: "01" , city: "01"},
{ location: "01" , city: "02"},
{ location: "03" , city: "03"},
{ location: "04" , city: "04"},
{ location: "01" , city: "01"},
{ location: "01" , city: "01"}
]

如何遍历数组Data[]并在locations[]中添加对象,而不会像第一个对象和后两个对象那样重复它

locations = [
{ location: "01" , city: "01"},
{ location: "01" , city: "02"},
{ location: "02" , city: "02"},
{ location: "03" , city: "03"},
{ location: "04" , city: "04"}
]

您可以使用Array.reduce()过滤掉重复项并获得唯一的对象数组,locations

var Data = [
{ location: "01" , city: "01"},
{ location: "01" , city: "02"},
{ location: "03" , city: "03"},
{ location: "04" , city: "04"},
{ location: "01" , city: "01"},
{ location: "01" , city: "01"}
];
var locations = Data.reduce((acc, obj)=>{
var exist = acc.find(({location, city}) => obj.location === location && obj.city === city);
if(!exist){
acc.push(obj);
}
return acc;
},[]);
console.log(locations);

可以完成这项工作

Data = [
{ location: "01", city: "01" },
{ location: "01", city: "02" },
{ location: "03", city: "03" },
{ location: "04", city: "04" },
{ location: "01", city: "01" },
{ location: "01", city: "01" }
]
const locations = Data.reduce( ( acc, d ) => {
if ( !acc.some( ( a ) => a.city == d.city && a.location == d.location ) ) {
acc.push( d )
}
return acc
}, [] )
console.log( locations )

您可以简单地使用forEach和推送值,并结合locationcity,并用"_"

var locations = {}
var Data = [
{ location: "01" , city: "01"},
{ location: "01" , city: "02"},
{ location: "03" , city: "03"},
{ location: "04" , city: "04"},
{ location: "01" , city: "01"},
{ location: "01" , city: "01"}
]
Data.forEach(val=>{ 
var key = val.location+"_"+val.city;
locations[key] = val;
});

locations = Object.values(locations);

console.log(locations);

可以使用.filter()方法和.map()方法的组合,如下所示:

var locations = Data.filter((d,i) => Data.map(dt => dt.location).indexOf(d.location) == i);

演示:

Data = [
{ location: "01" , city: "01"},
{ location: "01" , city: "02"},
{ location: "03" , city: "03"},
{ location: "04" , city: "04"},
{ location: "01" , city: "01"},
{ location: "01" , city: "01"}
];
var locations = Data.filter((d,i) => Data.map(dt => dt.location).indexOf(d.location) == i);
console.log(locations);

function unique(a, key) {
var seen = {};
return a.filter(function (item) {
item = key ? item[key] : item;
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
}
//var locations= unique(Data , "city");
var locations= unique(unique(Data , "city") , "location");

您可以简单地使用Array.filter()Set()(每个条目都设置了城市和位置的组合(。如果某个元素在 set 中不存在,则添加它并将该元素也添加到 set 中。请尝试以下操作:

let Data = [ { location: "01" , city: "01"}, { location: "01" , city: "02"}, { location: "03" , city: "03"}, { location: "04" , city: "04"}, { location: "01" , city: "01"}, { location: "01" , city: "01"} ];
let set = new Set();
let result = Data.filter((e)=>{
var key = e.location + "_" + e.city;
if(!set.has(key)){
set.add(key);
return true;
}
return false;
});
console.log(result);

您可以将位置名称作为键添加到对象以进行 uniqness 检查,然后在使用Array.reduce完成数组迭代后返回对象值。

var data = [{
location: "01",
city: "01"
},
{
location: "01",
city: "02"
},
{
location: "03",
city: "03"
},
{
location: "04",
city: "04"
},
{
location: "01",
city: "01"
},
{
location: "01",
city: "01"
}
]
var result = data.reduce((mem, cur) => {
var key = `{cur.location}--${cur.city}`
if (!mem[key]) {
mem[key] = cur;
}
return mem
}, {})
console.log(Object.values(result));

最新更新