我有一个包含n
数组的数组,每个数组都包含不同数量的字符串元素。
每个字符串都包含一个关键字,如evar#
,event#
,prop#
(其中#
是一个数字)。
我需要做以下事情:
以下是包含3个数组的数组的示例:
[
[
"overwrite value of evar1 with page_url_query 'int_cmp'",
"set event1 to custom value '1'",
"set event2 to custom value '1'",
"overwrite value of evar2 with page_url",
"overwrite value of evar3 with contextdata.user_id",
"set event4 to eventid",
"set event3 to eventid"
],
[
"overwrite value of prop3 with contextdata.phase",
"overwrite value of prop2 with contextdata.room",
"set event1 to custom value '1'",
"set event2 to eventid",
"overwrite value of evar5 with contextdata.queue",
"set event4 to eventid",
"overwrite value of evar6 with contextdata.audience",
"set event3 to eventid",
"set event5 to custom value '1'"
],
[
"overwrite value of evar4 with contextdata.no_challenges",
"overwrite value of prop3 with contextdata.user_type",
"overwrite value of evar7 with contextdata.interaction",
"set event2 to custom value '1'",
"set event3 to eventid",
"set event4 to eventid",
"set event1 to custom value '1'",
"set event5 to custom value '1'"
]
]
响应应包含以下数组:
[evar1, event2, evar2, evar3, prop3, prop2, evar6, event5, evar4, evar7]
如果我需要提供更多信息,请与我们联系。我对任何建议持开放态度......
LATER EDIT:
我实施了一个解决方案,但我认为它需要一些优化。如果有人帮助我,我将不胜感激......
答案 0 :(得分:0)
我实施了一个解决方案,丑陋,但显然它有效!有什么想法吗?
基本上,我找到了最大的数组,然后我将其元素与其他数组进行比较。如果有多个长度相同的数组
,不确定它是否可行
var allActions = [
[
"overwrite value of evar1 with page_url_query 'int_cmp'",
"set event1 to custom value '1'",
"set event2 to custom value '1'",
"overwrite value of evar2 with page_url",
"overwrite value of evar3 with contextdata.user_id",
"set event4 to eventid",
"set event3 to eventid"
],
[
"overwrite value of prop3 with contextdata.phase",
"overwrite value of prop2 with contextdata.room",
"set event1 to custom value '1'",
"set event2 to eventid",
"overwrite value of evar5 with contextdata.queue",
"set event4 to eventid",
"overwrite value of evar6 with contextdata.audience",
"set event3 to eventid",
"set event5 to custom value '1'"
],
[
"overwrite value of evar4 with contextdata.no_challenges",
"overwrite value of prop3 with contextdata.user_type",
"overwrite value of evar7 with contextdata.interaction",
"set event2 to custom value '1'",
"set event3 to eventid",
"set event4 to eventid",
"set event1 to custom value '1'",
"set event5 to custom value '1'"
]
]
var keyWords = ["evar","event","prop"];
var resultArray = allActions[0];
var arrLen = [];
var different = [];
for(var i = 0; i < allActions.length; i++) {
arrLen.push(allActions[i].length);
}
var max = Math.max.apply(null, arrLen)
var maxArray = arrLen.indexOf(max);
for(elem in allActions[maxArray]) {
for(var i = 0; i < allActions.length; i++) {
if(i !== maxArray) {
for(var j in allActions[i]) {
var mainElem = allActions[maxArray][elem];
var checkElem = allActions[i][j];
if(mainElem !== checkElem) {
for(var k = 0; k < keyWords.length; ++k) {
if( (index = mainElem.indexOf(keyWords[k])) !== -1) {
splittedStr = mainElem.substring(index, mainElem.length).split(' ', 1);
if(splittedStr[0].indexOf("evar") !== -1) {
if(different.indexOf(splittedStr[0]) == -1) {
different.push(splittedStr[0]);
}
}
if(splittedStr[0].indexOf("prop") !== -1) {
if(different.indexOf(splittedStr[0]) == -1) {
different.push(splittedStr[0]);
}
}
if(splittedStr[0].indexOf("event") !== -1) {
if(different.indexOf(splittedStr[0]) == -1) {
different.push(splittedStr[0]);
}
}
}
}
}
}
}
}
}
console.log(different);
答案 1 :(得分:0)
如果您对ES6没问题,可以按照以下方式执行功能
var data = [
[
"overwrite value of evar1 with page_url_query 'int_cmp'",
"set event1 to custom value '1'",
"set event2 to custom value '1'",
"overwrite value of evar2 with page_url",
"overwrite value of evar3 with contextdata.user_id",
"set event4 to eventid",
"set event3 to eventid"
],
[
"overwrite value of prop3 with contextdata.phase",
"overwrite value of prop2 with contextdata.room",
"set event1 to custom value '1'",
"set event2 to eventid",
"overwrite value of evar5 with contextdata.queue",
"set event4 to eventid",
"overwrite value of evar6 with contextdata.audience",
"set event3 to eventid",
"set event5 to custom value '1'"
],
[
"overwrite value of evar4 with contextdata.no_challenges",
"overwrite value of prop3 with contextdata.user_type",
"overwrite value of evar7 with contextdata.interaction",
"set event2 to custom value '1'",
"set event3 to eventid",
"set event4 to eventid",
"set event1 to custom value '1'",
"set event5 to custom value '1'"
]
],
interim = data.map(ds => ds.reduce((p,c) => p.concat(c.match(/[a-z]+\d/g)),[]));
result = [...interim.reduce((p,c) => p.filter(f => c.includes(f)))
.reduce((s,c) => (s.delete(c),s), interim.reduce((s,a) => a.reduce((ss,c) => ss.add(c),s),new Set()))];
console.log(result);
&#13;