请在下面找到我的JSON:我想识别唯一用户并获得相应的唯一用户值计数。例如:
userID: user1 有3条记录,我想形成如下的JSON:
预期结果JSON:
[ {
"userId" : "user1",
"success" : 1,
"error" : 0,
"cancel" : 0,
"visit" : 3,
"enroll" : 2,
"uninstall" : 0
}]
user1有1 成功,2 ENROLLED 和3 ** VISITED **,其余值 null 所以我们必须算作 0
我们必须为剩下的用户做同样的事情。
你能否帮助我使用javascript / jquery来实现这一目标。
主要JSON:
[ {
"userId" : "user1",
"success" : null,
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : "ENROLLED",
"uninstall" : null
}, {
"userId" : "user1",
"success" : null,
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : "ENROLLED",
"uninstall" : null
}, {
"userId" : "user1",
"success" : "SUCCESS",
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : null,
"uninstall" : null
}, {
"userId" : "user2",
"success" : "SUCCESS",
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : null,
"uninstall" : null
}, {
"userId" : "user2",
"success" : null,
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : "ENROLLED",
"uninstall" : null
}, {
"userId" : "user2",
"success" : null,
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : "ENROLLED",
"uninstall" : null
}, {
"userId" : "user2",
"success" : "SUCCESS",
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : null,
"uninstall" : null
}, {
"userId" : "user3",
"success" : "SUCCESS",
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : null,
"uninstall" : null
}, {
"userId" : "user3",
"success" : null,
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : "ENROLLED",
"uninstall" : null
}, {
"userId" : "user4",
"success" : null,
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : "ENROLLED",
"uninstall" : null
}, {
"userId" : "user4",
"success" : null,
"error" : null,
"cancel" : "CANCELLED",
"visit" : "VISITED",
"enroll" : null,
"uninstall" : null
}, {
"userId" : "user4",
"success" : null,
"error" : null,
"cancel" : "CANCELLED",
"visit" : "VISITED",
"enroll" : null,
"uninstall" : null
}, {
"userId" : "user4",
"success" : null,
"error" : null,
"cancel" : null,
"visit" : "VISITED",
"enroll" : "ENROLLED",
"uninstall" : null
} ]
答案 0 :(得分:0)
您需要在此处执行几个步骤,首先您需要将filter
数组缩小为仅匹配userId
的元素。
function isUser(userRecord){
return userRecord.userId === userId;
}
然后希望将map
值设置为通用数据格式,将您的值转换为简单的1
或0
。
function createSummaryRecords(userRecord){
return {
"userId" : userRecord.userId,
"success" : !userRecord.success ? 0 : 1,
"error" : !userRecord.error ? 0 : 1,
"cancel" : !userRecord.cancel ? 0 : 1,
"visit" : !userRecord.visit ? 0 : 1,
"enroll" : !userRecord.enroll ? 0 : 1,
"uninstall" : !userRecord.uninstall ? 0 : 1
};
}
然后希望reduce
数组到用户条目的摘要。
function sumarize(previousValue, currentValue){
return {
"userId" : currentValue.userId,
"success" : previousValue.success + currentValue.success,
"error" : previousValue.error + currentValue.error,
"cancel" : previousValue.cancel + currentValue.cancel,
"visit" : previousValue.visit + currentValue.visit,
"enroll" : previousValue.enroll + currentValue.enroll,
"uninstall" : previousValue.uninstall + currentValue.uninstall
}
}
链接示例:
allUsersEntries
.filter(isUser)
.map(createSummaryRecords)
.reduce(sumarize);
https://jsfiddle.net/po6nry25/1/
虽然具有自己属性的foreach
在这种情况下可能更有效,但这种方法允许您记录您的意图,并允许您改变您的实现以检查{{1}中的特定值步骤。