嗨,我无法在任何地方找到答案。
我在创造什么?
我正在创建数据库管理面板,前面是vue.js,后面是node.js + mySQL。
有4个数据库。每个数据库行都有phone_number
,e_mail
,created
,source_website
列。
我已经从所有数据库导入了记录。我将它们推入一个对象数组中。
我想要实现的目标是什么?
我想将记录与同一封电子邮件合并,并将每个重复记录中的每个值推送到一个合并记录。
预期产出
之前:
[
{
"number": "111111111",
"email": "[email protected]",
"created": "2019-02-18T15:01:18.000Z",
"website": "http://firstwebsite.com",
"id": 24,
},
{
"number": "111222222",
"email": "[email protected]",
"created": "2019-02-18T15:01:18.000Z",
"website": "http://secondwebsite.com",
"id": 24,
},
]
后:
[
{
"number": "111111111, 111222222",
"email": "[email protected]",
"created": "2019-02-18T15:01:18.000Z",
"website": "http://firstwebsite.com, http://secondwebsite.com",
"id": 24,
},
]
这可能会简化,但它正在发挥作用。
let data = [{
"number": "111111111",
"email": "[email protected]",
"created": "2019-02-18T15:01:18.000Z",
"website": "http://firstwebsite.com",
"id": 24,
}, {
"number": "452",
"email": "[email protected]",
"created": "2019-02-18T15:01:18.000Z",
"website": "http://first.com",
"id": 28,
}, {
"number": "111222222",
"email": "[email protected]",
"created": "2019-02-18T15:01:18.000Z",
"website": "http://secondwebsite.com",
"id": 24,
}, {
"number": "999",
"email": "[email protected]",
"created": "2019-05-18T15:01:18.000Z",
"website": "http://first.com",
"id": 29,
}];
// Start with an object because I will use email property as key
// for an easier checking of which email has already been looped through
let result = {};
for (let i in data) {
let item = data[i],
// Here the checking that I talked above
toPush = (result[item.email]) ? result[item.email] : {};
for (let property in item) {
let value = item[property];
if (!toPush[property]) {
toPush[property] = value;
} else {
// If the current value is not already in the value list,
// we add it after a ", "
if (`${toPush[property]}`.indexOf(value) === -1) {
toPush[property] += `, ${value}`;
}
}
}
result[item.email] = toPush;
}
// As we want an array as final result
result = Object.values(result);
console.log(result);
@ iArcadia,非常感谢你。你的回答非常有帮助。
我稍微修改了这个功能。新代码的主要特点是:
list_1
财产的新财产(例如list_2
,website
等)。它为新闻通讯系统管理员提供了将简报发送到不同列表的可能性。新功能如下:
mergeRecords() {
this.subscribers.forEach((subscriber) => {
if(subscriber.website === 'lodzkietargi') {
subscriber.lista_1 = 1;
}
else if(subscriber.website === 'targiwroclaw') {
subscriber.lista_2 = 1;
}
else if(subscriber.website === 'slaskietargi') {
subscriber.lista_3 = 1;
}
else if(subscriber.website === 'targi') {
subscriber.lista_4 = 1;
}
});
let result = {};
let itemId = 1;
for (let i in this.subscribers) {
let item = this.subscribers[i],
toPush = (result[item.email]) ? result[item.email] : {};
item.id = itemId
itemId += 1;
if (toPush.created > item.created) {
item.created = toPush.created;
}
for (let property in item) {
let value = item[property];
if (!toPush[property]) {
toPush[property] = value;
} else {
if (property === 'website') {
toPush.website += `, ${value}`;
}
}
}
result[item.email] = toPush;
}
result = Object.values(result);
this.subscribers = result;
console.log(result);
}
我仍然认为代码可能更好。有很多if语句。无论如何,它可能对某人有帮助。再一次,非常感谢你。你帮助了我很多。