2017-12-16 9 views
-1

類似の単語を配列の要素から削除する方法はありますか?類似の単語を配列の要素から削除する方法はありますか?

初期配列:

var arr = ["element first", "element second", "element third"]; 

望ましい結果:

var result = ["first", "second", "third"]; 

ありがとう!

+0

はい。何か試しましたか? – Li357

+1

「要素」は特に削除する部分ですか?または、「すべてのアイテムが何であれ、それが始まっている共有文字列」を削除する一般的な方法があるかどうかを尋ねていますか? – LJHarb

+0

質問はあまり指定されていません。 »同様の要素を定義する方法を定義してください:同じ文字列で開始しますか?またはどこにでもありますか?そして、それがすべてのアイテムまたはそれらのサブセットにのみ影響を与える場合に限りますか? –

答えて

1

あなたはすべての文字列の最初の単語を削除することができます。これは、これをしたいと思います。

var arr = ["element first", "element second", "element third"]; 

var output = []; 
for (var i =0; i < arr.length; i++) { 
    var items = arr[i].split(" "); 
    arr[i] = arr[i].replace(items[0] + " ", ""); 
    output[i] = arr[i]; 
} 

console.log(output); 

出力は希望の配列です。

0

注:ES6を使用します。

const arr = ["element first", "element second", "element third"]; 
 

 
const replaced = arr.map(el => el.replace('element ','')); 
 

 
console.log(replaced);

2

あなたは(同じインデックスで)共通の文字を見つけて、文字列の文字をフィルタリングすることができます。

var array = ["element first", "element second", "element third"], 
 
    common = array 
 
     .map(a => [...a]) 
 
     .reduce((a, b) => a.map((v, i) => v === b[i] ? v : null)) 
 
     .filter(Boolean), 
 
    result = array.map(a => [...a].filter((v, i) => v !== common[i]).join('')); 
 
    
 
console.log(result); 
 
console.log(common);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

あなたはarr配列に含まれる単語の辞書を作成し、辞書内の各エントリにインデックスを割り当てることができます。

var wordList = ["element first", "element second", "element third"] 
 
// this could be a object, but im going with a Map 
 
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map 
 
var wordMap = new Map() 
 

 
for(var i = 0; i < wordList.length; i++) { 
 
    var words = wordList[i].split(/\s/) 
 
    // go trough every (possible) word 
 
    for(var j = 0; j < words.length; j++) { 
 
    var word = words[j] 
 
    // check if the map already contains the word 
 
    if(!wordMap.has(word)) { 
 
     // the map doesn't contain the word, add it 
 
     wordMap.set(word, [i]) 
 
    } else { 
 
     // the map already contains the word, get the current indexes 
 
     var override = wordMap.get(word) 
 
     // add the current index to the list 
 
     override.push(i) 
 
     // update the map entry 
 
     wordMap.set(word, override) 
 
    } 
 
    } 
 
} 
 

 
var mapEntries = wordMap.entries() 
 
var iterator = mapEntries.next() 
 
// go trough every map entry 
 
while(!iterator.done) { 
 
    var word = iterator.value[0] 
 
    var indexes = iterator.value[1] 
 
    // there must be atleast one index 
 
    if(indexes.length !== 1) { 
 
    // there are multiple indexes 
 
    for(var k = 0; k < indexes.length; k++) { 
 
     // remove the word and assign the new string to the position of the old string 
 
     // the RegExp and the String.trim functions are used to remove unnecessary whitespace 
 
     wordList[k] = wordList[k].replace(new RegExp(word + '\\s?'), '').trim() 
 
    } 
 
    } 
 
    // get the next entry pair 
 
    iterator = mapEntries.next() 
 
} 
 

 
console.log(wordList)

0
  • 現在の文字列
  • の開始があり、文字列の新しい配列への配列の地図と一致する現在の接頭語のサブセットを確立するために、適切な末尾再帰関数を使用して、その共通のプレフィックスに配列を削減プレフィックス長を超えた文字のスライス

const arr = ["element first", "element second", "element third"], 
 
    pre = arr.reduce((pfx, b) => toPrefix(pfx, b)), 
 
    res = arr.map(a => a.slice(pre.length)); 
 

 
console.log("removed: %s\nresult: %s", JSON.stringify(pre), JSON.stringify(res)); 
 

 
function toPrefix(pfx, b, n = 0) { 
 
    return b[n] && pfx[n] === b[n] ? toPrefix(pfx, b, n+1) : b.slice(0, n); 
 
}

関連する問題