我有以下陣列:
['total.color.violet', 'total.color.violet.truefont', 'total.color.red', 'total.color.red.circle', 'total.color.red.circle.elefant', 'total.color.blue', 'total.color.yellow', 'total.color.yellow.dotted']. 正如我們所見,所有字串都有常量部分total.color和可變部分。我需要得到下一個結果:['total.color.violet', 'total.color.red', 'total.color.blue', 'total.color.yellow']- 洗掉復雜度超過“第三級”的字串,只留下“第二級”復雜度的字串。如果可能的話,請看一下這個演算法并給我任何提示。
uj5u.com熱心網友回復:
一個相對簡單的“點數”案例。
在這里,我使用正則運算式來確定.字串中出現的次數。'g' 標志回傳字串中的所有匹配項。
match可以在不匹配的情況下回傳 null ,因此|| []確保存在一個長度為零的陣列,以便我們可以訪問該屬性length。
所有具有 2 個或更少點的條目都保留在過濾后的陣列中。
let values = ['total.color.violet', 'total.color.violet.truefont', 'total.color.red', 'total.color.red.circle', 'total.color.red.circle.elefant', 'total.color.blue', 'total.color.yellow', 'total.color.yellow.dotted']
const output = values
.filter(value => (value.match(/\./g) || []).length <= 2)
console.log(output)
uj5u.com熱心網友回復:
使用Set洗掉欺騙,使用正則運算式查找字串match。
const arr=['total.color.violet','total.color.violet.truefont','total.color.red','total.color.red.circle','total.color.red.circle.elefant','total.color.blue','total.color.yellow','total.color.yellow.dotted'];
const set = new Set();
const re = /^total.color.[a-z] /;
for (const str of arr) {
set.add(str.match(re)[0]);
}
console.log([...set]);
uj5u.com熱心網友回復:
干得好。只需使用'level1.level2.level3'.split('.')將其分解為一個由 . 分隔的陣列即可.。
您可以使用 Set() 輕松洗掉重復項(區分大小寫),然后將其轉換回陣列。
let values = ['total.color.violet', 'total.color.violet.truefont', 'total.color.red', 'total.color.red.circle', 'total.color.red.circle.elefant', 'total.color.blue', 'total.color.yellow', 'total.color.yellow.dotted'];
let level123 = [];
let colorsOnly = [];
for(let value of values){
// Get each level (seperated by '.')
let level = value.split('.');
// Add each level (1-3)
level123.push(`${level[0]}.${level[1]}.${level[2]}`);
// Add only the colors
colorsOnly.push(level[2]);
}
// Remove duplication
level123 = [...new Set(level123)];
colorsOnly = [...new Set(colorsOnly)]
// Show the newly formatted values
console.log("Levels 1-3: ", level123);
console.log("Colors only: ", colorsOnly);
uj5u.com熱心網友回復:
最簡單的方法是根據每個單詞呼叫 split 的長度使用過濾器。 javascript拆分
一種簡單的方法是遍歷陣列并洗掉“ total.color”。使用切片或替換方法從每個字串中提取部分。然后,在第二次回圈中,如果字串包含“。”
有3個以上的級別:
- 字串替換
- 刺痛包括
- 更復雜的方法是使用正則運算式
uj5u.com熱心網友回復:
我們可以通過首先找到那些以您的前綴開頭的元素,然后為每個元素切掉前綴(加上一個額外的'.'),在每個 處拆分它',',獲取第一個結果并將其附加到前綴(再次,加上'.'. ) 然后我們通過將結果包裝在 Set 中并將其轉回陣列來獲取其中唯一的集合。它可能看起來像這樣:
const nextLevel = (strings, prefix) => [...new Set (
strings .filter (s => s .startsWith (prefix '.'))
.map (s => prefix '.' s .slice (prefix .length 1) .split ('.') [0])
)]
const strings1 = ['total.color.violet', 'total.color.violet.truefont', 'total.color.red', 'total.color.red.circle', 'total.color.red.circle.elefant', 'total.color.blue', 'total.color.yellow', 'total.color.yellow.dotted']
const prefix1 = 'total.color'
console .log (nextLevel (strings1, prefix1))
const strings2 = ['telex.fast', 'telex.fast.line.hope', 'total.fast.ring', 'telex.slow', 'total.slow.motion']
const prefix2 = 'telex'
console .log (nextLevel (strings2, prefix2))
.as-console-wrapper {max-height: 100% !important; top: 0}
一個有趣的替代方法是不提供前綴作為初始引數,而是直接從字串中提取它。我們可以這樣做:
const leveled = (ss, prefix = ss .reduce ((x, y, _, __, [a, b] = y .length < x .length ? [x, y] : [y, x]) =>
a .slice (0, [...a] .findIndex ((_, i) => a [i] !== b [i]))
)) => [...new Set (ss .map (s => s .substring (prefix .length) .split ('.') [0]))] .map (s => prefix s)
const input1 = ['total.color.violet', 'total.color.violet.truefont', 'total.color.red', 'total.color.red.circle', 'total.color.red.circle.elefant', 'total.color.blue', 'total.color.yellow', 'total.color.yellow.dotted']
console .log (leveled (input1))
const input2 =['telex.fast', 'telex.fast.line.hope', 'total.fast.ring', 'telex.slow', 'total.slow.motion']
console .log (leveled (input2))
.as-console-wrapper {max-height: 100% !important; top: 0}
但這不會telex以與上述相同的方式捕獲該示例,因為它考慮了所有級別,并且沒有通用前綴。上面的例子給了我們['telex.fast', 'telex.slow'],這個例子會給我們['telex', 'total']。哪個更合適取決于您的需求。
我們還應該注意,由于該技術使用reduce沒有初始值,因此它不適用于空值串列。
最后,應該清理這個版本,提取輔助函式。我可能更喜歡這樣寫:
const sharedPrefix = (a, b) => a.length < b .length
? sharedPrefix (b, a)
: a .slice (0, [...a] .findIndex ((_, i) => a [i] !== b [i]))
const commonPrefix = (ss) => ss .reduce (sharedPrefix)
const leveled = (ss, prefix = commonPrefix (ss)) =>
[...new Set (ss .map (s => s .substring (prefix .length) .split ('.') [0]))] .map (s => prefix s)
(這幾乎沒有經過測驗。)
uj5u.com熱心網友回復:
您可以使用Array.filter()方法通過一行代碼簡單地實作這一點。
演示:
const arr = [
'total.color.violet',
'total.color.violet.truefont',
'total.color.red',
'total.color.red.circle',
'total.color.red.circle.elefant',
'total.color.blue',
'total.color.yellow',
'total.color.yellow.dotted'
];
const prefix = 'total.color';
const res = arr.filter(item => item.indexOf(prefix) === 0).filter(item => item.split('.').length === 3);
console.log(res);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/488637.html
標籤:javascript 节点.js 数组 细绳 算法
上一篇:Javascript重構-消除依賴于具有相似元素的串列物件的代碼重復
下一篇:C#洗掉兩個特定字符之間
