鍍金池/ 問(wèn)答/HTML/ js 判斷數(shù)組中每一個(gè)元素是否有相同的部分

js 判斷數(shù)組中每一個(gè)元素是否有相同的部分

[
"http://information_schema/CHARACTER_SETS",
 "http://information_schema/COLLATIONS", 
 "http://information_schema/COLLATION_CHARACTER_SET_APPLICABILITY", 
 "http://information_schema/COLUMNS",
 "http://ghy",
 "http://performance_schema/accounts",
 "http://performance_schema/cond_instances", 
 "http://performance_schema/events_stages_current",
 "http://performance_schema/events_stages_history", 
 "http://test1/test1"
]

判斷數(shù)組中這些元素有沒(méi)有相同的部分(除了//),每一個(gè)都含有相同的字符串則返回true,沒(méi)有則返回false。
比如:

[
"http://information_schema/CHARACTER_SETS",
 "http://information_schema/COLLATIONS", 
 "http://information_schema/COLLATION_CHARACTER_SET_APPLICABILITY", 
 "http://information_schema/COLUMNS"
]

數(shù)組中四個(gè)元素均含有information_schema,如果數(shù)組只有這四個(gè)元素,則返回true。
如果是對(duì)于整個(gè)數(shù)組而言,無(wú)法人為的切割呢

場(chǎng)景:

clipboard.png

如果,這樣勾選,不需要對(duì)返回的數(shù)組元素處理:

[
"ghy",
"performance_schema",
"test"
]

如果勾選了兩個(gè)數(shù)據(jù)庫(kù)test1和test4其中一個(gè)表,則返回false,見(jiàn)下圖

clipboard.png

如果勾選了兩個(gè)數(shù)據(jù)庫(kù)test1和test4,其中數(shù)據(jù)庫(kù)test1的表test1和test2都勾選了,同時(shí)數(shù)據(jù)庫(kù)test4的表course勾選了,這樣也要返回false,見(jiàn)下圖
如果數(shù)據(jù)庫(kù)的表全部勾選,只處理數(shù)據(jù)庫(kù)即可

[
"test1",
"course"
]

clipboard.png

回答
編輯回答
離魂曲

假設(shè)待比較數(shù)組str長(zhǎng)度為5;

  1. 求str[0],str[1]的最長(zhǎng)公共子串lcs;

  2. 循環(huán)求lcs與str[i]的最大公共子串lcs

  3. if(lcs.length > 0) return true else return false;

function LCSFunc(str1, str2){
    //...
}
let lcs = LCSFunc(str[0],str[1]);
for(let i=2; i<str.length; i++){
    lcs = LCSFunc(lcs, str[i])
    if(lcs.length > 0) return true else return false;

}
2017年12月22日 09:58
編輯回答
離殤

如果只是按\\\分段比較就比較簡(jiǎn)單

let hasSameSegment = list => {
    let [first_segments, ...rest] = list.map(str => str.split(/\/+/).filter(str => str));
    return rest.every(segment => {
        first_segments = first_segments.filter(seg => segment.includes(seg));
        return first_segments.length > 0;
    });
}
2017年11月21日 23:36
編輯回答
巫婆

鑒于不可能匹配任意字段,因?yàn)?a','b'也算字段
結(jié)合你給的數(shù)組,默認(rèn)取'/'劃分的字符串進(jìn)行匹配
能夠做到即使不在相同的位置也能匹配
亮點(diǎn)在于全字符串用match來(lái)匹配第一個(gè)元素里的目標(biāo)字符串

function sameStrHasFound(arr) {
  var result = false
  if (arr.length <= 1) return false //小于2為false
  //全部字符串連接起來(lái)
  var allStr = arr.join('!(@*!!)#(*!)@*')
  //第一個(gè)字符串用/來(lái)分隔,同時(shí)去除空字符串的情況,并循環(huán)各個(gè)目標(biāo)字符串
  arr[0].split('/').filter(str => { return str != '' }).forEach(str => {
    if (result) return
    //求目標(biāo)字符串匹配個(gè)數(shù)
    var match = allStr.match(new RegExp(str, 'gi'))
    //如果匹配個(gè)數(shù)是原數(shù)組長(zhǎng)度則為true
    if (match !== null && match.length === arr.length) result = true
  });
  return result
}
var list1 = [
  "http://information_schema/CHARACTER_SETS",
  "http://information_schema/COLLATIONS",
  "http://information_schema/COLLATION_CHARACTER_SET_APPLICABILITY",
  "http://information_schema/COLUMNS",
  "http://ghy"
]
var list2 = [
  "http://information_schema/CHARACTER_SETS",
  "http://information_schema/COLLATIONS",
  "http://information_schema/COLLATION_CHARACTER_SET_APPLICABILITY",
  "http://information_schema/COLUMNS",
]
var list3 = [
  "http://CHARACTER_SETS//information_schema",
  "http://information_schema/COLLATIONS",
  "http://information_schema/COLLATION_CHARACTER_SET_APPLICABILITY",
  "http://information_schema/COLUMNS",
]
console.log(sameStrHasFound(list1), sameStrHasFound(list2), sameStrHasFound(list3))
2017年10月25日 19:30
編輯回答
忘了我
const data1 = [
    "http://information_schema/CHARACTER_SETS",
    "http://information_schema/COLLATIONS",
    "http://information_schema/COLLATION_CHARACTER_SET_APPLICABILITY",
    "http://information_schema/COLUMNS",
    "http://ghy",
    "http://performance_schema/accounts",
    "http://performance_schema/cond_instances",
    "http://performance_schema/events_stages_current",
    "http://performance_schema/events_stages_history",
    "http://test1/test1"
];

const data2 = [
    "http://information_schema/CHARACTER_SETS",
    "http://information_schema/COLLATIONS",
    "http://information_schema/COLLATION_CHARACTER_SET_APPLICABILITY",
    "http://information_schema/COLUMNS"
];

const data3 = [
    "http://CHARACTER_SETS//information_schema",
    "http://information_schema/COLLATIONS",
    "http://information_schema/COLLATION_CHARACTER_SET_APPLICABILITY",
    "http://information_schema/COLUMNS",
];

function hasSameSegment(array) {
    // 如果 length < 2 顯然沒(méi)啥好比較的
    if (array.length < 2) {
        return true;
    }

    const [firstEl, ...rest] = array;

    // 把第一項(xiàng)中各段保存在一個(gè)數(shù)組中
    let segments = firstEl.split("/").filter(s => s);

    rest.forEach(el => {
        // 先生成一個(gè) map 便于檢索
        const map = el.split("/")
            .filter(s => s)
            .reduce((all, s) => {
                all[s] = s;
                return all;
            }, {});
        
        // 如果 segments 中某一段不存在于 map 中
        // 說(shuō)明至少有一個(gè)列表元素不包含該段
        segments = segments.filter(s => map[s]);
    });

    // segments 最后如果還留下有一些段,
    // 那這些段肯定是每個(gè)列表元素中都包含的
    return !!segments.length;
}

[data1, data2, data3].forEach(d => console.log(hasSameSegment(d)));
2017年2月18日 02:52
編輯回答
凹凸曼
function equals(arr){
          // 定義一個(gè)空數(shù)組來(lái)接收
        var one = [];
        // 需要將原來(lái)的數(shù)組做一下格式化以便處理
           var a_arr = arr.toString().split('//');
        var b_arr = a_arr.filter((item1,index) => {
            return index>0;
        });
        // 將處理好的數(shù)組遍歷,并將元素的前半部分放進(jìn)一個(gè)新的數(shù)組中
        for(var i in b_arr) {
            var    key = b_arr[i].substring(0, b_arr[i].indexOf('/'));
            one.push(key);
        }
          // console.log(one[0]);
        // console.log(one);

        // 通過(guò)Array.prototype.every() 方法進(jìn)行對(duì)數(shù)組所有元素測(cè)試
        return one.every(item => item === one[0]);
    }

經(jīng)過(guò)測(cè)試均能通過(guò),上面的大哥比我厲害,我想了一晚上,但是我倆思路是一樣的,希望采納~

2017年12月21日 05:24