按字母数排序。检查以相同字母开头的单词。检查一个单词是否完全出现在另一个单词中。当两个单词都以相同的字母开头时。如果它完全存在。只显示第一个单词和忽略第二个单词
var arraylist = ["running", "walking", "standing", "writing", "waiting", "sleeping", "reading", "washing", "sitting", "riding", "shopping", "singing", "painting", "watching", "swimming", "pulling", "smoking", "pushing", "drinking", "dancing", "cooking", "crying ", "eating", "smiling","run", "walk", "push", "cook", "cry", "eat"];
使用javascript / jquery预期输出:
我需要删除这些词:跑步,走路,推,做饭,哭,吃。
var arrayList = ["run", "walk", "standing", "writing", "waiting", "sleeping", "reading", "washing", "sitting", "riding", "shopping", "singing", "painting", "watching", "swimming", "pulling", "smoking", "push", "drinking", "dancing", "cook", "cry", "eat", "smiling","run", "walk", "push", "cook", "cry", "eat"];
答案 0 :(得分:1)
对于@Grimbode提到的案例 来自OP
检查一个单词是否完全出现在另一个单词
中
所以微笑 完全存在 微笑
所以这个解决方案可能会有所帮助
function filter(arr) {
arr.sort(function(a, b) {
return a.length - b.length;
})
return arr.reduce(function(acc, el) {
if (!acc.some(function(e) {
return el.startsWith(e);
})) {
acc.push(el);
}
return acc;
}, []);
}
注意:来自mdn
的旧浏览器的Polyfillif (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.lastIndexOf(searchString, position) === position;
};
}
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.lastIndexOf(searchString, position) === position;
};
}
var arraylist = ["running", "walking", "standing", "writing", "waiting", "sleeping", "reading", "washing", "sitting", "riding", "shopping", "singing", "painting", "watching", "swimming", "pulling", "smoking", "pushing", "drinking", "dancing", "cooking", "crying ", "eating", "smiling", "run", "walk", "push", "cook", "cry", "eat"];
function filter(arr) {
arr.sort(function(a, b) {
return a.length - b.length;
})
return arr.reduce(function(acc, el) {
if (!acc.some(function(e) {
return el.startsWith(e);
})) {
acc.push(el);
}
return acc;
}, []);
}
document.getElementById('before').innerHTML = JSON.stringify(arraylist);
document.getElementById('result').innerHTML = JSON.stringify(filter(arraylist));

<div><span>initial array:</span><span id="before"></span>
<div>
<div><span>result array:</span><span id="result"></span>
<div>
&#13;
答案 1 :(得分:0)
检查一下。虽然这在@Grimbode
提到的情况下不起作用
var sentence = "Did Marying go to the store today?";
var arraylist = ["running", "walking", "standing", "writing", "waiting", "sleeping", "reading", "washing", "sitting", "riding", "shopping", "singing", "painting", "watching", "swimming", "pulling", "smoking", "pushing", "drinking", "dancing", "cooking", "crying ", "eating", "smiling","run", "walk", "push", "cook", "cry", "eat"];
function myFunction() {
$.each(arraylist, function(index,item){
$.each(arraylist,function(indexInner, item1){
if( index != indexInner && item1 && item && item1.substring(0,(item.length)) == item){
arraylist[indexInner] = arraylist[index];
arraylist[index] = false;
}
});
});
arraylist = cleanArray(arraylist);
document.getElementById("demo").innerHTML = arraylist;
}
function cleanArray(actual){
var newArray = new Array();
for(var i = 0; i<actual.length; i++){
if (actual[i]){
newArray.push(actual[i]);
}
}
return newArray;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Click the button to add elements to the array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
&#13;