[
"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)景:
如果,這樣勾選,不需要對(duì)返回的數(shù)組元素處理:
[
"ghy",
"performance_schema",
"test"
]
如果勾選了兩個(gè)數(shù)據(jù)庫(kù)test1和test4其中一個(gè)表,則返回false,見(jiàn)下圖
如果勾選了兩個(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"
]
假設(shè)待比較數(shù)組str長(zhǎng)度為5;
求str[0],str[1]的最長(zhǎng)公共子串lcs;
循環(huán)求lcs與str[i]的最大公共子串lcs
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;
}
如果只是按\\
或\
分段比較就比較簡(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;
});
}
鑒于不可能匹配任意字段,因?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))
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)));
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ò),上面的大哥比我厲害,我想了一晚上,但是我倆思路是一樣的,希望采納~
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。