我的数组为空
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"}
]
答案 0 :(得分:3)
您可以使用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);
答案 1 :(得分:1)
您可以简单地使用docker service create --limit-cpu 2 nginx
并结合forEach
和location
并以city
分隔的值来推送值
"_"
答案 2 :(得分:1)
可以做
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 )
答案 3 :(得分:0)
您可以结合使用.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);
答案 4 :(得分:0)
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");
答案 5 :(得分:0)
您可以简单地使用Array.filter()
和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);
答案 6 :(得分:0)
您可以将位置名称作为键添加到对象以进行唯一性检查,然后在使用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));