Javascript / jQuery在数组的其余部分中查找并删除数组值

时间:2015-11-13 03:24:44

标签: javascript jquery arrays

好的,不知道如何提出这个问题,但是我试图将值集合到一个数组中,但是每个值可能在其余值上有重复(或附加)的字符串,我相信它们总是建立在彼此之上...

这就是我的意思。

如果我输出一个数组,它将是这样的:

[0] => This is a string.
[1] => here's another string. This is a string.
[2] => And now there's a third string.  here's another string. This is a string.
[3] => Here's a string that might not follow the pattern.  This is a string.

我想最终输出的是

[0] => This is a string.
[1] => here's another string.
[2] => And now there's a third string.
[3] => Here's a string that might not follow the pattern.

基本上,任何字符串都不能包含任何其他字符串中的重复文本。

不确定我是否有道理。

编辑:

以下是完整的故事 - 我通过PHP IMAP使用AJAX脚本收集电子邮件。我从每封电子邮件中获取了正文的文字,但不幸的是,无法通过所有电子邮件服务找到引用的文字。所以我所做的就是让每个电子邮件正文显示,并且我已经删除了任何额外的字符(例如行引号>>字符)。

我现在要做的是:从第一封电子邮件开始,检查其他电子邮件,然后删除第一个字符串,然后使用第二封电子邮件,检查其余电子邮件并删除那个字符串,依此类推。

它并不完美,但它会缩短很多信息。

4 个答案:

答案 0 :(得分:2)

在使用空字符串首次替换新数组中的每个字符串之后,使用旧数组中的每个字符串构建一个新数组。

filestore.allow({
    insert: function () {
        return true;
    },
    update: function () {
        return true;
    },
    remove: function () {
        return true;
    },
    download: function(){
        return true;
    }
});

答案 1 :(得分:1)

var strings = [
    "This is a string.",
    "here's another string. This is a string.",
    "And now there's a third string.  here's another string. This is a string.",
    "Here's a string that might not follow the pattern.  This is a string."
];

var i, j, pos;
for (i = 0; i < strings.length; i += 1) {
    for (j = 0; j < strings.length; j += 1) {
        if (i === j) {
            continue;
        }
        pos = strings[i].indexOf(strings[j]);
        if (pos !== -1) {
            strings[i] = strings[i].substr(0, pos) + strings[i].substr(pos + strings[j].length);
        } 
    }
}

console.log(strings);
output: [
  "This is a string.",
  "here's another string. ",
  "And now there's a third string.  ",
  "Here's a string that might not follow the pattern.  "
];

请注意,此代码不会替换多次出现。如果你想要,添加一个while循环。

答案 2 :(得分:1)

修改 如果字符串具有特殊的正则表达式字符,这将导致问题。没有正则表达式的JS全局repalce可以利用joinvar arr = [ 'This is a string.', "here's another string. This is a string.", "And now there's a third string. here's another string. This is a string.", "Here's a string that might not follow the pattern. This is a string." ]; for (var i = 0; i < arr.length; i++) { for (j = 0; j < arr.length; j++) { if (j !== i) { arr[i] = arr[i].split(arr[j]).join(""); } } } console.log(arr); [ "This is a string.", "here's another string.", "And now there's a third string.", "Here's a string that might not follow the pattern." ] ,(http://www.adequatelygood.com/JS-Find-and-Replace-with-SplitJoin.html)见下文:

Javascript需要正则表达式来进行全局替换,所以像这样

{{1}}

它应该替换所有出现的

答案 3 :(得分:0)

使用如下。使用jquery的.map函数迭代数组。代码如下:

var arr = [
           'This is a string.',
           "here's another string. This is a string.",
           "And now there's a third string.  here's another string. This is a string.",
           "Here's a string that might not follow the pattern.  This is a string."
         ];

     var out=$.map(arr,function (i,v){
         return i.substr(0,i.indexOf(".") + 1);
     });
     console.log("value is"+out);