jQuery的发现和阵列的其余部分删除数组

jQuery的发现和阵列的其余部分删除数组

本文介绍了使用Javascript / jQuery的发现和阵列的其余部分删除数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,不知道怎么问这个问题,但我试图收集值到一个数组,但每个值可能复制(或附加)字符串的值的其余部分,我相信他们总是建立在彼此的顶部上...

下面就是我的意思。

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

  [0] =>这是一个字符串。
[1] =>这里的另一个字符串。这是一个字符串。
[2] =>现在还出现了第三个字符串。这里的另一个字符串。这是一个字符串。
[3] =>下面是可能不遵循这种模式的字符串。这是一个字符串。

我想什么,最终输出

  [0] =>这是一个字符串。
[1] =>这里的另一个字符串。
[2] =>现在还出现了第三个字符串。
[3] =>下面是可能不遵循这种模式的字符串。

在任何其他字符串基本上,任何字符串不能有重复的文字。

不知道如果我做的感觉。

编辑:

下面是完整的故事 - 我收集通过PHP IMAP电子邮件与AJAX脚本。我已经得到了身体的每个电子邮件的内容,但遗憾的是,有没有办法找到通过所有电子邮件服务所引用的文字。所以我所做的就是让每封电子邮件正文显示,我已经摆脱了任何额外的字符(如网上报价>>字符)。

我想现在要做的是:与第一封电子邮件开始,检查其他电子邮件,并删除第一个字符串,如果它在那里,然后用第二封电子邮件,检查电子邮件的其余部分,删除该字符串,等等。

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


解决方案

此建立使用每个字符串老数组中后首次更换一个新的数组,在该字符串,每个字符串以空字符串的新阵列中的

\r
\r

VAR ARR = [\r
  这是一个字符串。,\r
  这里的另一个字符串。这是一个字符串。\r
  现在有一个第三个字符串。这里的另一个字符串。这是一个字符串。\r
  这是有可能不遵循这种模式的字符串。这是一个字符串。\r
];\r
\r
变量超出= arr.reduce(功能(W,S){\r
  w.push(w.reduce(函数(S1,E){\r
    返回s1.replace(即,);\r
  },S).trim());\r
  返回瓦;\r
},[]);\r
\r
警报(出);

\r

\r
\r

Ok, not sure how to ask this question, but I'm trying to gather values into an array, but each value MAY have duplicated (or appended) strings on the rest of the values and I believe they are always built on top of each other...

Here's what I mean.

if I were to output an array, it would be something like this:

[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.

What I'd like to finally output is

[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.

Basically, Any string can not have duplicated text in any other string.

Not sure if I'm making sense.

EDIT:

Here's the full story - I'm gathering emails through PHP IMAP with an AJAX script. I've gotten the text of the body from each email, but unfortunately, there's no way to find the quoted text through all email services. So what I've done is make each email body show and I've gotten rid of any extra characters (like line quote >> characters).

What I want to do now is: starting with the first email, check the other emails, and remove the first string if it's in there, then use the second email, check the rest of the emails and remove that string, and so on.

It's not perfect, but it will shorten a lot of the messages.

解决方案

This builds a new array using each string in the old array after first replacing, in that string, each string in the new array with empty string.

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 = arr.reduce(function(w, s) {
  w.push(w.reduce(function(s1, e) {
    return s1.replace(e, "");
  }, s).trim());
  return w;
}, []);

alert(out);

这篇关于使用Javascript / jQuery的发现和阵列的其余部分删除数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:57