我尝试通过以下方式获取重复记录,但我得到了唯一的过滤记录意味着只有 uniq 记录获取。
var filtered = _.uniqWith(form_data.itemRows, _.isEqual);
数组列表:
[
{
"PlanId": "",
"Id": "",
"Type": "Beverages",
"Beverages": "2019/12/7 11:40",
"Frequency": 1
},
{
"PlanId": "",
"Id": "",
"Type": "Medication",
"Beverages": "2019/12/7 11:30",
"Frequency": 1
},
{
"PlanId": "",
"Id": "",
"Type": "Medication",
"Beverages": "2019/12/7 11:30",
"Frequency": 1
}
]
类型、饮料、频率明智地获取重复记录。
最佳答案
这样就可以了
let jsonArray = [
{
"PlanId": "",
"Id": "",
"Type": "Beverages",
"ActivityTime": "2019/12/7 11:40",
"Frequency": 1
},
{
"PlanId": "",
"Id": "",
"Type": "Medication",
"ActivityTime": "2019/12/7 11:30",
"Frequency": 1
},
{
"PlanId": "",
"Id": "",
"Type": "Medication",
"ActivityTime": "2019/12/7 11:30",
"Frequency": 1
}
]
let Result = _.filter(
_.uniq(
_.map(jsonArray, function (item) {
if (_.filter(jsonArray, {
PlanId: item.PlanId,
Id: item.Id,
Type: item.Type,
ActivityTime: item.ActivityTime,
Frequency: item.Frequency
}).length > 1) {
return item
}
return false;
})),
function (value) { return value; });
console.log(Result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
关于javascript - 使用 lodash 获取数组中的重复对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59208001/