我有一系列Javascript对象,如下所示。
[{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "40",
"rate": "20",
"amount": "200",
"vat": "60",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "mike@test.com",
"fn": "Mike",
"sn": "Mann",
"phone": "01233xxxxx",
"hours": "50",
"rate": "70",
"amount": "500",
"vat": "90",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "fred@test.com",
"fn": "Fred",
"sn": "Frogg",
"phone": "01233xxxxx",
"hours": "80",
"rate": "90",
"amount": "800",
"vat": "100",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "90",
"rate": "30",
"amount": "900",
"vat": "120",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}]
我理想的是将相同值的电子邮件(电子邮件)分组到自己的子对象数组中,例如,如果你查看上面的数组,你会看到我有两个条目给同一个人Alex McPherson。我想要做的是下面尽可能移动并组合成一个子数组,并且对于存在多次的任何其他值都是相同的。
[[{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "40",
"rate": "20",
"amount": "200",
"vat": "60",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "90",
"rate": "30",
"amount": "900",
"vat": "120",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}],
[{
"email": "mike@test.com",
"fn": "Mike",
"sn": "Mann",
"phone": "01233xxxxx",
"hours": "50",
"rate": "70",
"amount": "500",
"vat": "90",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}],
[{
"email": "fred@test.com",
"fn": "Fred",
"sn": "Frogg",
"phone": "01233xxxxx",
"hours": "80",
"rate": "90",
"amount": "800",
"vat": "100",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}]]
我似乎无法绕过阵列。
答案 0 :(得分:2)
您可以在哈希表上使用闭包来获取相同的电子邮件地址及其项目。
Dim DateYesterday As Date = Date.Today.AddDays(<insert number of days>).ToString("yyyy-MM-dd")
&#13;
var data = [{ email: "alex@test.com", fn: "Alex", sn: "McPherson", phone: "01233xxxxx", hours: "40", rate: "20", amount: "200", vat: "60", agency: "test", start: "08/06/2017", end: "10/06/2017" }, { email: "mike@test.com", fn: "Mike", sn: "Mann", phone: "01233xxxxx", hours: "50", rate: "70", amount: "500", vat: "90", agency: "test", start: "08/06/2017", end: "10/06/2017" }, { email: "fred@test.com", fn: "Fred", sn: "Frogg", phone: "01233xxxxx", hours: "80", rate: "90", amount: "800", vat: "100", agency: "test", start: "08/06/2017", end: "10/06/2017" }, { email: "alex@test.com", fn: "Alex", sn: "McPherson", phone: "01233xxxxx", hours: "90", rate: "30", amount: "900", vat: "120", agency: "test", start: "08/06/2017", end: "10/06/2017" }],
result = data.reduce(function (hash) {
return function (r, o) {
if (!hash[o.email]) {
hash[o.email] = [];
r.push(hash[o.email]);
}
hash[o.email].push(o)
return r;
};
}(Object.create(null)), []);
console.log(result);
&#13;
答案 1 :(得分:1)
您可以使用ES6地图收集每封电子邮件的数据,然后从该地图中提取结果值(假设输入了data
):
Array.from(
data.reduce(
(acc, o) => (acc.get(o.email).push(o), acc),
new Map(data.map( o => [o.email, []] ))
), ([key, value]) => value
)
var data = [{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "40",
"rate": "20",
"amount": "200",
"vat": "60",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}, {
"email": "mike@test.com",
"fn": "Mike",
"sn": "Mann",
"phone": "01233xxxxx",
"hours": "50",
"rate": "70",
"amount": "500",
"vat": "90",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}, {
"email": "fred@test.com",
"fn": "Fred",
"sn": "Frogg",
"phone": "01233xxxxx",
"hours": "80",
"rate": "90",
"amount": "800",
"vat": "100",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}, {
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "90",
"rate": "30",
"amount": "900",
"vat": "120",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}];
var result = Array.from(
data.reduce(
(acc, o) => (acc.get(o.email).push(o), acc),
new Map(data.map( o => [o.email, []] ))
), ([key, value]) => value
);
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 2 :(得分:1)
const groupBy = (objectList, groupingProp) => {
// Create a dictionnary of groups.
const dict = data.reduce((acc, entry) => {
const groupKey = entry[groupingProp];
acc[groupKey] = acc[groupKey] || [];
acc[groupKey].push(entry);
return acc;
}, {})
// Transform it back into a list of groups.
return Object.values(dict);
}
// The data.
const data = [{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "40",
"rate": "20",
"amount": "200",
"vat": "60",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "mike@test.com",
"fn": "Mike",
"sn": "Mann",
"phone": "01233xxxxx",
"hours": "50",
"rate": "70",
"amount": "500",
"vat": "90",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "fred@test.com",
"fn": "Fred",
"sn": "Frogg",
"phone": "01233xxxxx",
"hours": "80",
"rate": "90",
"amount": "800",
"vat": "100",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "90",
"rate": "30",
"amount": "900",
"vat": "120",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}];
console.log(groupBy(data, 'email'));
&#13;
答案 3 :(得分:0)
这是一个有效的例子。我们使用ES6数组.reduce()
函数。
var data = [{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "40",
"rate": "20",
"amount": "200",
"vat": "60",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "mike@test.com",
"fn": "Mike",
"sn": "Mann",
"phone": "01233xxxxx",
"hours": "50",
"rate": "70",
"amount": "500",
"vat": "90",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "fred@test.com",
"fn": "Fred",
"sn": "Frogg",
"phone": "01233xxxxx",
"hours": "80",
"rate": "90",
"amount": "800",
"vat": "100",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "90",
"rate": "30",
"amount": "900",
"vat": "120",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}]
const grouped = data.reduce((previous, current) => {
if (!previous[current.email]) {
const found = previous.find(element => element.email === current.email);
if (found) {
const index = previous.indexOf(found);
previous.splice(index,1);
// We add always at the top
previous.unshift([found, current]);
} else {
previous.push(current);
}
}
return previous;
}, [])
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 4 :(得分:0)
let emails = [{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "40",
"rate": "20",
"amount": "200",
"vat": "60",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "mike@test.com",
"fn": "Mike",
"sn": "Mann",
"phone": "01233xxxxx",
"hours": "50",
"rate": "70",
"amount": "500",
"vat": "90",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "fred@test.com",
"fn": "Fred",
"sn": "Frogg",
"phone": "01233xxxxx",
"hours": "80",
"rate": "90",
"amount": "800",
"vat": "100",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{
"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "90",
"rate": "30",
"amount": "900",
"vat": "120",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}];
let emails_obj = [];
let output_array = [];
emails.forEach(function(obj) {
if (emails_obj[obj.email] == undefined) {
emails_obj[obj.email] = []
emails_obj[obj.email].push(obj);
} else {
emails_obj[obj.email].push(obj);
}
})
for (var key in emails_obj) {
output_array.push(emails_obj[key]);
}
console.log(output_array);
答案 5 :(得分:0)