简体   繁体   English

从多个数组中查找差异和缺少的元素

[英]Find differences and missing elements from multiple arrays

I have an array that contains n arrays and each of these arrays contain a different number of string elements. 我有一个包含n数组的数组,每个数组都包含不同数量的字符串元素。

Each string contains a key word like evar# , event# , prop# (where # is a number). 每个字符串包含一个关键字,例如evar#event#prop# (其中#是数字)。

I need to do the following: 我需要执行以下操作:

  • return all the key words from a string that is different with at least one of the other arrays 从与其他数组中至少一个数组不同的字符串中返回所有关键字
  • return the key words that does not exist in at least one of the other arrays. 返回在至少一个其他数组中不存在的关键字。

Here is an example with an array that contains 3 arrays: 这是一个包含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'"
    ]
]

And the response should contain the following array: 响应应包含以下数组:

[evar1, event2, evar2, evar3, prop3, prop2, evar6, event5, evar4, evar7]

Please let me know if I need to provide more information. 如果需要提供更多信息,请告诉我。 I'm open to any suggestions... 我愿意接受任何建议...

LATER EDIT: 之后编辑:

I implemented a solution, but I think that it needs some optimization. 我实现了一个解决方案,但我认为它需要一些优化。 I would appreciate if someone helps me with that... 如果有人帮助我,我将不胜感激。

I implemented a solution, ugly, but apparently it works! 我实施了一个丑陋的解决方案,但显然可行! Any thoughts about it? 有什么想法吗?

Basically, I find the largest sized array and then I compare its elements with the other arrays. 基本上,我找到了最大的数组,然后将其元素与其他数组进行比较。 Not sure if it will work if there are multiple arrays with the same length 不知道如果有多个相同长度的数组是否会起作用

 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); 

If you are OK with ES6 you might do functionally as follows 如果您对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(/[az]+\\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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM