[英]What is the best way to find if there are any duplicates in nested child property in a JavaScript Object?
I have an array of objects(Vue 3 prop) like below.我有一个对象数组(Vue 3 prop),如下所示。 The array is for
room
objects.该数组用于
room
对象。 Each room contains adults
and childs
array with adult
and child
objects.每个房间都包含
adults
和childs
数组以及adult
和child
对象。 Now I need to mark the duplicate names ( first
and last
name together) by adding a property name error
(Shown in example).现在我需要通过添加属性名称
error
来标记重复的名称( first
和last
一起)(如示例所示)。
[
{
"RoomType":{ },
"Price": { },
"Messages": [],
"CancellationPolicyStatus": "",
"adults": [
{ "title": "Mr.", "first": "John", "last": "Doe"},
{ "title": "Mrs.", "first": "Jane", "last": "Doe"}
],
"children": [
{ "title": "Ms.", "first": "Jane", "last": "Doe"},
{ "title": "Mr.", "first": "Joe", "last": "Doe" }
]
},
{
"RoomType":{ },
"Price": { },
"Messages": [],
"CancellationPolicyStatus": "",
"adults": [
{ "title": "Mr.", "first": "Johny", "last": "Doe",},
{ "title": "Mrs.", "first": "Jane", "last": "Doe",}
],
"children": [
{ "title": "Ms.", "first": "Jane", "last": "Doe"},
{ "title": "Mr.", "first": "Jui", "last": "Doe"}
]
},
]
After I run the function or code in question.在我运行有问题的函数或代码之后。 The resulting array should look like below.
结果数组应如下所示。
[
{
"RoomType":{ },
"Price": { },
"Messages": [],
"CancellationPolicyStatus": "",
"adults": [
{ "title": "Mr.", "first": "John", "last": "Doe"},
{ "title": "Mrs.", "first": "Jane", "last": "Doe", "error": "Duplicate name, please update." }
],
"children": [
{ "title": "Ms.", "first": "Jane", "last": "Doe", "error": "Duplicate name, please update." },
{ "title": "Mr.", "first": "Joe", "last": "Doe" }
]
},
{
"RoomType":{ },
"Price": { },
"Messages": [],
"CancellationPolicyStatus": "",
"adults": [
{ "title": "Mr.", "first": "Johny", "last": "Doe", },
{ "title": "Mrs.", "first": "Jane", "last": "Doe", "error": "Duplicate name, please update." }
],
"children": [
{ "title": "Ms.", "first": "Jane", "last": "Doe", "error": "Duplicate name, please update." },
{ "title": "Mr.", "first": "Jui", "last": "Doe" }
]
},
]
Update:更新:
This is my first question to Stack Overflow, even though I am regular user of the platform for last 7+ years.这是我向 Stack Overflow 提出的第一个问题,尽管过去 7 年多来我一直是该平台的常客。
I am overwhelmed by the responses and definitely will go through each solution.我对这些反应感到不知所措,并且肯定会通过每个解决方案。
I am not a JS developer and tried to make a solution (inspired by我不是 JS 开发人员并试图提出解决方案(灵感来自
vanowm's comment) that now looks like below. vanowm 的评论)现在如下所示。 I believe the responses have a better solution.
我相信这些回应有更好的解决方案。
const isDuplicate = function (names, person) {
let result = false;
names.forEach(function (name) {
if(name.first === person.first && name.last === person.last){
result = true;
}
});
return result;
}
const validateNames = function () {
let names = [];
rooms.forEach(function (room) {
room.adults.forEach(function (adult) {
if (isDuplicate(names, adult)) {
adult.error = 'Duplicate name, please update.'
// I can do this because it is a Vue Reactive.
} else {
adult.error = ''
names.push(adult);
}
})
room.childs.forEach(function (child) {
if (isDuplicate(names, child)) {
child.error = 'Duplicate name, please update.'
} else {
child.error = ''
names.push(child);
}
})
});
};```
Here's my naive attempt这是我天真的尝试
I assumed you want to find duplicates among adults separately from duplicates among children - it's not clear since the only duplicate is Jane Doe and she appears twice as an adult and twice as a child!我假设您想在成人中找到重复项与儿童中的重复项分开查找 - 目前尚不清楚,因为唯一的重复项是 Jane Doe,她作为成人出现两次,作为儿童出现两次!
const data = [ { RoomType: {}, Price: {}, Messages: [], CancellationPolicyStatus: "", adults: [ { title: "Mr.", first: "John", last: "Doe" }, { title: "Mrs.", first: "Jane", last: "Doe" }, ], childs: [ { title: "Ms.", first: "Jane", last: "Doe" }, { title: "Mr.", first: "Joe", last: "Doe" }, ], }, { RoomType: {}, Price: {}, Messages: [], CancellationPolicyStatus: "", adults: [ { title: "Mr.", first: "Johny", last: "Doe" }, { title: "Mrs.", first: "Jane", last: "Doe" }, ], childs: [ { title: "Ms.", first: "Jane", last: "Doe" }, { title: "Mr.", first: "Jui", last: "Doe" }, ], }, ]; const store = {}; const findDupe = (o, type) => { const key = [o.first, o.last].join(); const tbl = (store[type] = store[type] || {}); if (!tbl[key]) { tbl[key] = [o]; return; } if (tbl[key].length === 1) { tbl[key][0].error = "Duplicate name, please update."; } o.error = "Duplicate name, please update."; tbl[key].push(o); }; data.forEach((record) => { record.adults.forEach((adult) => findDupe(adult, "adults")); record.childs.forEach((child) => findDupe(child, "childs")); }); console.log(JSON.stringify(data, null, 4));
There are probably better ways to do this, but this should suit your needs.可能有更好的方法可以做到这一点,但这应该适合您的需求。 You'll need to loop over both of the arrays checking the names of each object.
您需要遍历两个数组,检查每个对象的名称。 Set the error for both
adult
and child
if you come across a match.如果遇到匹配项,请为
adult
和child
设置错误。
Should also be worth noting that your parent array of these objects was not named (unless it is named props, I don't use Vue so I don't know if that is a convention or not) so you would have to loop through every object in the parent array to do this.还应该值得注意的是,这些对象的父数组没有被命名(除非它被命名为 props,我不使用 Vue,所以我不知道这是否是一个约定)所以你必须遍历每个父数组中的对象来执行此操作。
const namesArray = [];
// Loop over the adults array. Store the names and index to check against later
adults.forEach((adult, index) => {
let fullName = adult.first + adult.last;
namesArray.push({name: fullName, index: index});
}
// Loop over the child array
// If the name of a child is found in the namesArray, set the error for the child and the corresponding adult
children.forEach(child => {
let fullName = child.first + child.last;
namesArray.forEach(nameObject => {
if(nameObject.name === fullName) {
child.error = 'Duplicate name, please update.';
adults[nameObject.index].error = 'Duplicate name, please update.';
}
}
}
Create two objects (database) where first and last names will be stored, iterate your objects and check if first/last name exists in the database, if not, add them to the database:创建两个将存储名字和姓氏的对象(数据库),迭代您的对象并检查数据库中是否存在名字/姓氏,如果不存在,将它们添加到数据库中:
const arr = [ { "RoomType":{ }, "Price": { }, "Messages": [], "CancellationPolicyStatus": "", "adults": [ { "title": "Mr.", "first": "John", "last": "Doe"}, { "title": "Mrs.", "first": "Jane", "last": "Doe"} ], "childs": [ { "title": "Ms.", "first": "Jane", "last": "Doe"}, { "title": "Mr.", "first": "Joe", "last": "Doe" } ] }, { "RoomType":{ }, "Price": { }, "Messages": [], "CancellationPolicyStatus": "", "adults": [ { "title": "Mr.", "first": "Johny", "last": "Doe",}, { "title": "Mrs.", "first": "Jane", "last": "Doe",} ], "childs": [ { "title": "Ms.", "first": "Jane", "last": "Doe"}, { "title": "Mr.", "first": "Jui", "last": "Doe"} ] }, ]; function check(arr) { //we'll store names for both adults and children in these objects const first = {}, last = {}; return arr.map(room => { const checkDup = person => { //check if this first/last name already exists in our database if (first[person.first] !== undefined && last[person.last] !== undefined) { //set error property in current person object person.error = "Duplicate name, please update."; //set error property in the original person object first[person.first].error = person.error; } else { //store names in the database first[person.first] = person; last[person.last] = person; } return person; } room.adults.forEach(checkDup); room.childs.forEach(checkDup); return room; }); } console.log(check(arr)); /* [ { "RoomType":{ }, "Price": { }, "Messages": [], "CancellationPolicyStatus": "", "adults": [ { "title": "Mr.", "first": "John", "last": "Doe"}, { "title": "Mrs.", "first": "Jane", "last": "Doe", "error": "Duplicate name, please update." } ], "childs": [ { "title": "Ms.", "first": "Jane", "last": "Doe", "error": "Duplicate name, please update." }, { "title": "Mr.", "first": "Joe", "last": "Doe" } ] }, { "RoomType":{ }, "Price": { }, "Messages": [], "CancellationPolicyStatus": "", "adults": [ { "title": "Mr.", "first": "Johny", "last": "Doe", }, { "title": "Mrs.", "first": "Jane", "last": "Doe", "error": "Duplicate name, please update." } ], "childs": [ { "title": "Ms.", "first": "Jane", "last": "Doe", "error": "Duplicate name, please update." }, { "title": "Mr.", "first": "Jui", "last": "Doe" } ] }, ] */
not to be a grammar nazzy, but it's
children
, notchilds
不要成为一个语法笨蛋,但这是
children
,不是childs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.