// function addSpaceBeforeCapsAndNumbers(str) {
// return str
// // 在大写字母前添加空格,但不包括字符串开头的大写字母
// .replace(/([A-Z])/g, ' $1')
// // 删除由于首字母大写而产生的开头空格
// .trim()
// // 在数字前添加空格
// .replace(/(\d+)/g, ' $1')
// // 删除可能因前一个步骤而重复的空格
// .replace(/\s+/g, ' ');
// }
// // 示例使用
// console.log(addSpaceBeforeCapsAndNumbers('BergamTower1')); // "Bergam Tower 1"
// console.log(addSpaceBeforeCapsAndNumbers('AuroraPool2')); // "Aurora Pool 2"
function transformData(inputData) {
const result = {};
for (const key in inputData) {
if (inputData.hasOwnProperty(key)) {
// 提取事件类型和事件名称
const [eventType, ...eventNameParts] = key.split('_');
const eventName = eventNameParts.join('_').trim(); // 使用 trim() 处理可能的空格
// 构造与 relationData 中相匹配的事件名称
const eventKey = eventName.replace(/\s+/g, '_').toLowerCase(); // 替换空格并转换为小写
// 初始化或更新事件数据
if (!result[eventKey]) {
result[eventKey] = { event: eventKey, num: 0, qrcode: 0 };
}
if (eventType === 'click') {
result[eventKey].num += inputData[key];
} else if (eventType === 'showqrcode') {
result[eventKey].qrcode += inputData[key];
}
}
}
return Object.values(result);
}
// 示例输入
const inputData = {
"click_navbar_homepage": 5,
"click_notice_category _notice": 1,
"click_notice_detail_AdultYogaInterestClass": 1,
"showqrcode_notice_detail_AdultYogaInterestClass": 1,
"click_form_detail_AcknowledgementOfItemsBorrowing": 1,
"showqrcode_form_detail_0_AcknowledgementOfItemsBorrowing": 1,
"showqrcode_form_detail_1_AcknowledgementOfItemsBorrowing": 1,
"click_booking_detail_GamesWorld": 1,
"showqrcode_booking_detail_GamesWorld": 1,
"click_wayfinding_Tower_BergamTower1": 1,
"click_wayfinding_PoolsideClub_AuroraPool": 1,
"click_wayfinding_PoolsideClub_SerenityGym/GardenGym": 1,
"child_pdf_1_220": 4,
"常用表格": 2,
"click_from_275958": 1,
"click_from_270043": 2,
"click_from_270043_NOL,3,270043,1": 1,
"click_from_270043_NOL,3,270043,2": 1,
"预定设施": 4,
"设施type_110": 2,
"click_notice_detail_Handie Robot Loan Delivery Service Suspension": 64,
"click_navbar_food": 7,
"click_navbar_notice": 9,
"child_food_2": 1,
"click_navbar_wayfinding": 5,
"click_navbar_form": 6,
"click_navbar_booking": 4,
"click_navbar_transport": 4,
"click_navbar_community": 4,
"click_navbar_customerService": 2,
"click_notice_detail_Adult Yoga Interest Class": 7,
"click_notice_category_Notice": 5,
"click_notice_category_Clubhouse": 5,
"click_notice_category_Industry Committee Info": 4,
"click_notice_category_Other": 4,
"click_notice_detail_Crime Preventive Measures": 1,
"click_form_detail_借用物品簽收承諾書": 3,
"click_form_detail_Acknowledgement of Items Borrowing": 1,
"showqrcode_form_detail_Acknowledgement of Items Borrowing": 1,
"click_form_detail_Application for taking up of consumership of Water Supplies Department ": 1,
"showqrcode_form_detail_Application for taking up of consumership of Water Supplies Department ": 1,
"showqrcode_form_detail_借用物品簽收承諾書": 1,
"click_booking_detail_Enchanted Tunes- Piano Room": 3,
"showqrcode_booking_detail_Enchanted Tunes- Piano Room": 2,
"click_food_category_Breakfast": 2,
"click_food_category_Lunch": 2,
"click_wayfinding_Tower_Bergen Tower 1": 2,
"click_wayfinding_Tower_Bergen Tower 2": 1,
"click_wayfinding_Poolside Club_Garden Grill": 2,
"click_wayfinding_Poolside Club_Adventure Park": 1,
"click_transport_page_LightRail": 2,
"click_transport_page_MTR": 3,
"click_transport_page_Bus": 4,
"click_transport_page_Taxi": 2,
"click_transport_page_MiniBus": 2,
"click_transport_detail_ParingInfo": 2,
"click_transport_detail_BusStopLocation": 2,
"click_community_detail_Tuen Mun Park": 3,
"click_community_detail_Por Lo Shan": 1,
"click_notice_detail_Phase 2 Testing of Fire Service Sysytem": 1,
"showqrcode_notice_detail_Phase 2 Testing of Fire Service Sysytem}": 1,
"click_notice_detail_Chinese Calligraphy Class": 1,
"click_notice_detail_Interest class": 1,
"click_form_detail_搬屋/送貨車輛入閘申請表": 1,
"showqrcode_form_detail_搬屋/送貨車輛入閘申請表": 1,
"click_form_detail_預約家居維修增值服務申請表": 1,
"showqrcode_form_detail_預約家居維修增值服務申請表": 1,
"click_booking_detail_Games World": 1,
"click_booking_detail_Garden Grill": 1,
"click_booking_detail_Meet N Greet": 1,
"click_food_category_TEA TIME": 1,
"click_food_category_DINNER COMBO": 1,
"click_food_category_Chef Recommended": 1,
"click_wayfinding_Skywalk_Artful Mind": 1,
"click_community_detail_Ching Chung Koon": 1
};
// 调用函数并输出结果
const transformedData = transformData(inputData);
// console.log(transformedData);
function updateRelationData(relationData, eventCounts) {
// 将 eventCounts 转换为以事件名称为键的对象
const countsMap = eventCounts.reduce((acc, curr) => {
acc[curr.event] = curr;
return acc;
}, {});
// 遍历 relationData 的每个类别
Object.keys(relationData).forEach(category => {
relationData[category].forEach(item => {
// 获取与当前事件匹配的计数数据
const eventData = countsMap[item.event];
// 更新 num 和 qrcode
item.num = eventData ? eventData.num : 0;
item.qrcode = eventData ? eventData.qrcode : 0;
});
});
return relationData;
}
const relationData = {
navbar: [
{
event: 'navbar_homepage',
name: '打開主頁',
num: 0,
qrcode: 0
},
{
event: 'navbar_notice',
name: '打開物業通告',
num: 0,
qrcode: 0
},
{
event: 'navbar_booking',
name: '打開預定設施',
num: 0,
qrcode: 0
},
{
event: 'navbar_food',
name: '打開美食',
num: 0,
qrcode: 0
},
{
event: 'navbar_wayfinding',
name: '打開地圖',
num: 0,
qrcode: 0
},
{
event: 'navbar_transport',
name: '打開交通資訊',
num: 0,
qrcode: 0
},
{
event: 'navbar_community',
name: '打開社區',
num: 0,
qrcode: 0
},
{
event: 'navbar_customerService',
name: '打開客服',
num: 0,
qrcode: 0
}
],
notice: [
{
event: 'notice_category_notice',
name: '通告',
num: 10,
qrcode: 0
},
{
event: 'notice_category_clubHouse',
name: '會所',
num: 0,
qrcode: 0
},
{
event: 'notice_category_IndustryCommitteeInfo',
name: '業委會資訊',
num: 0,
qrcode: 0
},
{
event: 'notice_category_other',
name: '其它',
num: 0,
qrcode: 0
}
],
noticeDetail: [
{
event: 'notice_detail_pdfname1',
name: 'pdf_name',
num: 0,
qrcode: 0
}
],
tableDetail: [
{
event: 'notice_category_other',
name: 'from_name',
num: 0,
qrcode: 0
}
],
booking: [
{
event: 'booking_detail_bookingName1',
name: 'bookingName1',
num: 0,
qrcode: 0
}
],
food: [
{
event: 'food_category_Breakfast',
name: '早餐',
num: 0,
qrcode: 0
},
{
event: 'food_category_Lunch',
name: '午餐',
num: 0,
qrcode: 0
},
{
event: 'booking_detail_TeaTime',
name: '下午茶',
num: 0,
qrcode: 0
},
{
event: 'booking_detail_DinnerCombo',
name: '晚餐組合',
num: 0,
qrcode: 0
},
{
event: 'booking_detail_ChefRecommended',
name: '廚師推介',
num: 0,
qrcode: 0
}
],
wayfinding: [
{
event: 'click_wayfinding_Tower_Bergam Tower 1',
name: 'Bergam Tower 1',
num: 0,
qrcode: 0
}
],
transport: [
{
event: 'transport_page_LightRail',
name: '輕鐵page',
num: 0,
qrcode: 0
},
{
event: 'transport_page_MTR',
name: '港鐵page',
num: 0,
qrcode: 0
},
{
event: 'transport_page_Taxi',
name: '的士page',
num: 0,
qrcode: 0
},
{
event: 'transport_page_Bus',
name: '巴士page',
num: 0,
qrcode: 0
},
{
event: 'transport_page_MiniBus',
name: '小巴page',
num: 0,
qrcode: 0
},
{
event: 'transport_detail_ParingInfo',
name: '停車場資訊',
num: 0,
qrcode: 0
},
{
event: 'transport_detail_BusStopLocation',
name: '巴士站位置',
num: 0,
qrcode: 0
}
],
communicaty: [
{
event: 'community_detail_Tuen Mun Park',
name: 'Tuen Mun Park',
num: 0,
qrcode: 0
}
]
}
// 调用函数并输出结果
const updatedRelationData = updateRelationData(relationData, transformedData);
console.log(updatedRelationData);
console