搜索和过滤数组

时间:2016-07-15 06:18:33

标签: javascript arrays

假设我有一个这样的数组:

var arr = ['hello, my', 'hello, my name is', 'hello, my name is newton', 'hello, his', 'hello, his name is', 'hello, his name is pluto', 'hello, she is britney'];

我想将其过滤为:

var arr = ['hello, my name is newton', 'hello, his name is pluto', 'hello, she is britney'];

我不知道如何设置它,但条件就像是在其他元素上可以找到数组元素的字符串然后它应该被删除。喜欢'你好,我的'可以在下一个元素中找到...'你好,我的名字是'所以'你好,我的'应该删除。

我尝试过滤的实际数组是:

var arr = ['11 22 13', '11 22 13 34', '11 22 13 34 15', '11 22 13 34 35', '11 22 23', '11 22 23 34', '11 22 23 34 15', '11 22 23 34 35', '31 22 13', '31 22 13 34', '31 22 13 34 15', '31 22 13 34 35', '31 22 23', '31 22 23 34', '31 22 23 34 15', '31 22 23 34 35'];

我试图把它分成不同的组,但我仍然不知道如何处理它。无论如何我还会表现出来:

var threelink = [];
var fourlink = [];
var fivelink = [];

    for(var i=0; i < arr.length; i++){
        if(arr[i].length>8&&arr[i].length<12){
            fourlink.push(arr[i]);
        }
        else if(arr[i].length>11){
            fivelink.push(arr[i]);
        }
        else { 
            threelink.push(arr[i]);
        }
    }

5 个答案:

答案 0 :(得分:3)

我猜你想要

arr.filter(function(e, i, a) {
  return !a.some(function(e2) {
    return e2 !== e && e2.includes(e);
  });
})

这表示过滤数组,只保留其中包含NO(!some)其他元素的元素(但不等于它)。

ES6箭头功能更加紧凑:

arr.filter((e, i, a) => !a.some(e2 => e2 !== e && e2.includes(e)));

var arr = ['hello, my', 'hello, my name is', 'hello, my name is newton', 'hello, his', 'hello, his name is', 'hello, his name is pluto', 'hello, she is britney'];

var filtered = arr.filter((e, i, a) => !a.some(e2 => e2 !== e && e2.includes(e)));

console.log(filtered);

答案 1 :(得分:1)

您可以使用Array#reduceArray#filter来检查结果集中是否有字符串过滤掉,以及是否要将实际值插入到结果集中。

这适用于未分类的数据。

var arr = ['hello, my', 'hello, my name is', 'hello, my name is newton', 'hello, his', 'hello, his name is', 'hello, his name is pluto', 'hello, she is britney', 'abc', 'ab'],
    result = arr.reduce(function (r, a, i) {
        var push = true;
        r = r.filter(function (b) {
            push = push && b.indexOf(a) === -1;
            return a.indexOf(b) === -1;
        });
        push && r.push(a);
        return r;
    }, []);

console.log(result);

答案 2 :(得分:1)

普通的旧嵌套循环解决方案,可能比此线程中的替换更快,因为内部循环查看数组的缩小部分:

function uniqueContents(arr) {
    var work = arr.slice(), result = [],
        i, j, l = arr.length, found;

    work.sort(function (a, b) {
        return a.length > b.length;
    });

    for (i = 0; i < l; i++) {
        if (!work[i]) break;
        found = false;
        for (j = i + 1; j < l; j++) {
            if (!work[j]) break;
            found = work[j].indexOf(work[i]) > -1;
            if (found) break;
        }
        if (!found) result.push(work[i]);
    }
    return result;
}

除此之外,这是无副作用(不会扭曲输入)和容忍null / undefined值。

答案 3 :(得分:0)

如果您只想删除array1中未显示在array2中的所有元素,您可以使用以下内容:

var array1 = ['hello, my', 'hello, my name is', 'hello, my name is newton', 'hello, his', 'hello, his name is', 'hello, his name is pluto', 'hello, she is britney'];
var array2 = ['hello, my name is newton', 'hello, his name is pluto', 'hello, she is britney'];

for(var i = 0; i < array.lengh; i++){
    if(array2.indexOf(array1[i]) == -1){
        array1.splice(i, 1);
    }
}

让我们希望我能正确理解你的问题并帮助

答案 4 :(得分:0)

这个怎么样?

var arr = ['11 22 13', '11 22 13 34', '11 22 13 34 15', '11 22 13 34 35', '11 22 23', '11 22 23 34', '11 22 23 34 15', '11 22 23 34 35', '31 22 13', '31 22 13 34', '31 22 13 34 15', '31 22 13 34 35', '31 22 23', '31 22 23 34', '31 22 23 34 15', '31 22 23 34 35'];

arr.sort();
var filterArray = new Array();
for(var i = 0 ; i < arr.length; i++) {
    if(i + 1 == arr.length) {
    filterArray.push(arr[i]);
  } else {
    if(arr[i+1].startsWith(arr[i])) {
        continue;
    } else {
        filterArray.push(arr[i]);
    }
  }
}
alert(filterArray);

JSFiddle