[
"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ù)組中這些元素有沒有相同的部分(除了//),每一個(gè)都含有相同的字符串則返回true,沒有則返回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ù)組而言,無法人為的切割呢
場景:
如果,這樣勾選,不需要對(duì)返回的數(shù)組元素處理:
[
"ghy",
"performance_schema",
"test"
]
如果勾選了兩個(gè)數(shù)據(jù)庫test1和test4其中一個(gè)表,則返回false,見下圖
如果勾選了兩個(gè)數(shù)據(jù)庫test1和test4,其中數(shù)據(jù)庫test1的表test1和test2都勾選了,同時(shí)數(shù)據(jù)庫test4的表course勾選了,這樣也要返回false,見下圖
如果數(shù)據(jù)庫的表全部勾選,只處理數(shù)據(jù)庫即可
[
"test1",
"course"
]
假設(shè)待比較數(shù)組str長度為5;
求str[0],str[1]的最長公共子串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;
}鑒于不可能匹配任意字段,因?yàn)?a','b'也算字段
結(jié)合你給的數(shù)組,默認(rèn)取'/'劃分的字符串進(jìn)行匹配
能夠做到即使不在相同的位置也能匹配
亮點(diǎn)在于全字符串用match來匹配第一個(gè)元素里的目標(biāo)字符串
function sameStrHasFound(arr) {
var result = false
if (arr.length <= 1) return false //小于2為false
//全部字符串連接起來
var allStr = arr.join('!(@*!!)#(*!)@*')
//第一個(gè)字符串用/來分隔,同時(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ù)組長度則為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 顯然沒啥好比較的
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 中
// 說明至少有一個(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ù)組來接收
var one = [];
// 需要將原來的數(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);
// 通過Array.prototype.every() 方法進(jìn)行對(duì)數(shù)組所有元素測試
return one.every(item => item === one[0]);
}
經(jīng)過測試均能通過,上面的大哥比我厲害,我想了一晚上,但是我倆思路是一樣的,希望采納~
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國制造2025”,實(shí)現(xiàn)中華民族偉大復(fù)興的升級(jí)產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(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ū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。