根据 2 键从数组中删除重复对象
remove duplicate object from array according 2 key
我有这样的对象数组,我想根据 2 键删除重复项,
在我的数据中,我想删除具有相同 room_id
和 mdate
的项目,
在下面的代码中,对象 4 和对象 5 具有相同的 room_id 和 mdate,所以我应该删除其中一个,有什么解决方案吗?
我有删除重复对象的功能,但它可以检查一个键
function getUnique(arr, comp) {
arr.map()
const unique = arr
.map(e => e[comp])
// store the keys of the unique objects
.map((e, i, final) => final.indexOf(e) === i && i)
// eliminate the dead keys & store unique objects
.filter(e => arr[e])
.map(e => arr[e]);
return unique;
}
arr = [{
cond: 2
extra_fee: 20000
kind: ""
mdate: "2019-12-15"
price: "100000"
room_id: 1
},
{
cond: 2
extra_fee: 20000
kind: ""
mdate: "2019-12-10"
price: "100000"
room_id: 1
},
{
cond: 2
extra_fee: 20000
kind: "" object 3 <-------------- is same - have same mdate and room_id
mdate: "2019-12-10"
price: "100000"
room_id: 2
},
{
cond: 2
extra_fee: 20000
kind: ""
mdate: "2019-12-10"
price: "100000"
room_id: 2 object 4 <-------------- are same - have same mdate and room_id
},
{
cond: 2
extra_fee: 20000
kind: ""
mdate: "2019-12-10"
price: "100000"
room_id: 7
}
]
试试这个:
function getUnique(arr, comp) {
let tmpObj = {};
let unique = [];
for (let i = 0; i < arr.length; i++) {
if (tmpObj[arr[i][comp]] === undefined) {
tmpObj[arr[i][comp]] = arr[i][comp];
unique.push(arr[i]);
}
}
return unique;
}
let arr = [{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-15",
price: "100000",
room_id: 1
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 1
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 2
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 2
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 7
}
]
console.log(getUnique(arr, "room_id"));
这将删除重复项,
let arr = [{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-15",
price: "100000",
room_id: 1
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 1
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 2
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 2
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 7
}
]
const arrayWithoutDuplicates = arr.filter((v,i,a)=>a.findIndex(t=>(t.room_id=== v.room_id && t.mdate===v.mdate))===i)
console.log(arrayWithoutDuplicates)
我有这样的对象数组,我想根据 2 键删除重复项,
在我的数据中,我想删除具有相同 room_id
和 mdate
的项目,
在下面的代码中,对象 4 和对象 5 具有相同的 room_id 和 mdate,所以我应该删除其中一个,有什么解决方案吗?
我有删除重复对象的功能,但它可以检查一个键
function getUnique(arr, comp) {
arr.map()
const unique = arr
.map(e => e[comp])
// store the keys of the unique objects
.map((e, i, final) => final.indexOf(e) === i && i)
// eliminate the dead keys & store unique objects
.filter(e => arr[e])
.map(e => arr[e]);
return unique;
}
arr = [{
cond: 2
extra_fee: 20000
kind: ""
mdate: "2019-12-15"
price: "100000"
room_id: 1
},
{
cond: 2
extra_fee: 20000
kind: ""
mdate: "2019-12-10"
price: "100000"
room_id: 1
},
{
cond: 2
extra_fee: 20000
kind: "" object 3 <-------------- is same - have same mdate and room_id
mdate: "2019-12-10"
price: "100000"
room_id: 2
},
{
cond: 2
extra_fee: 20000
kind: ""
mdate: "2019-12-10"
price: "100000"
room_id: 2 object 4 <-------------- are same - have same mdate and room_id
},
{
cond: 2
extra_fee: 20000
kind: ""
mdate: "2019-12-10"
price: "100000"
room_id: 7
}
]
试试这个:
function getUnique(arr, comp) {
let tmpObj = {};
let unique = [];
for (let i = 0; i < arr.length; i++) {
if (tmpObj[arr[i][comp]] === undefined) {
tmpObj[arr[i][comp]] = arr[i][comp];
unique.push(arr[i]);
}
}
return unique;
}
let arr = [{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-15",
price: "100000",
room_id: 1
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 1
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 2
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 2
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 7
}
]
console.log(getUnique(arr, "room_id"));
这将删除重复项,
let arr = [{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-15",
price: "100000",
room_id: 1
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 1
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 2
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 2
},
{
cond: 2,
extra_fee: 20000,
kind: "",
mdate: "2019-12-10",
price: "100000",
room_id: 7
}
]
const arrayWithoutDuplicates = arr.filter((v,i,a)=>a.findIndex(t=>(t.room_id=== v.room_id && t.mdate===v.mdate))===i)
console.log(arrayWithoutDuplicates)